dousi6303 2015-12-29 08:32
浏览 66
已采纳

致命错误:在null上调用成员函数prepare()

code:

function insertheadright($name) {
    $str1 = xss($name);
    $sql = "INSERT INTO `nav-menu` (`id`, `Head-categories`, `RightMenu`, `LeftMenu`, `Sub-categories`, `Show-sub`, `Show-head`, `keywords`, `description`) VALUES (NULL, ?, '1', '0', '0', '0', '1', '', '')";
    $result = $connect->prepare($sql);
    $result->bindValue(1, $str1);
    $query = $result->execute();
    if ($query) {
        $num = 1;
        return $num;
    } else {
        $num = 0;
        return $num;
    }
}

code 2:

$object = insertheadright($_POST["nav-name"]);
if (isset($object)) {
    if ($object == 1)
        echo "<div class=ok>منو با موفقیت افزوده شد</div>";
    else
        echo '<div class="error">مشکل در ثبت فهرست</div>';
}

in error:

Notice: Undefined variable: connect in /home/user/domains/site.com/public_html/inc/nav/function-nav.php on line 8 Fatal error: Call to a member function prepare() on null in /home/designpr/domains/shrg.ir/public_html/inc/nav/function-nav.php on line 8

  • 写回答

1条回答 默认 最新

  • douke3442 2015-12-29 08:38
    关注

    Make the connect variable accessible to your function like so:

    function insertheadright($name) {
        global $connect;
    

    or, you could pass it as an argument to the function:

    function insertheadright($connect, $name) {
    

    Depending on your code, one might be better then the other but as long as you remain consistent you'll be okay.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?