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 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题