SQL Update - 在非对象上调用成员函数bind_param()

时间:2014-04-12 13:14:44

标签: php mysql mysqli

我正在上课,我有一个用户设置页面,管理员可以在其中修改设置。

当我点击更新按钮时,我返回此错误:

Call to a member function bind_param() on a non-object

这是我认为错误来源的代码:

<?php
$records = array();

if (!empty($_POST)) {
    if (isset($_POST['site_name'], $_POST['header_text'], $_POST['footer_copyright'], $_POST['allow_robot_name'], $_POST['default_robot_name'])) {   
        $site_name          = $_POST['site_name'];
        $header_text        = $_POST['header_text'];
        $footer_copyright   = $_POST['footer_copyright'];
        $allow_robot_name   = $_POST['allow_robot_name'];
        $default_robot_name = $_POST['default_robot_name'];

        if (!empty($site_name) && !empty($header_text) && !empty($footer_copyright) && !empty($allow_robot_name) && !empty($default_robot_name)) {
            $insert = $db->prepare("UPDATE user_settings (site_name, header_text, footer_copyright, allow_robot_name, default_robot_name) VALUES (?, ?, ?, ?, ?)");
            $insert->bind_param('sssss', $site_name, $header_text, $footer_copyright, $allow_robot_name, $default_robot_name);

            if ($insert->execute()) {
                header('Location: index.php');
                die();
            }
        }
    }
}

if ($results = $db->query("SELECT * FROM user_settings")) {
    if ($results->num_rows) {
        while ($row = $results->fetch_object()) {
        $records[] = $row;
        }

        $results->free();
    }
}

?>

这是db_connect.php中的数据库连接代码:

<?php
require_once('../db_config.php');

$db = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);

if ($db->connect_errno) {
    die('Sorry, we are currently experiencing some problems.');
}
?>

有谁知道我收到此错误的原因?

1 个答案:

答案 0 :(得分:2)

您的更新声明可能应该是:

UPDATE user_settings SET site_name = ?, header_text = ?, footer_copyright = ?, allow_robot_name = ?, default_robot_name = ?

通常在更新中,有一些WHERE条款也是有意义的,但我不知道您的用例到底是什么。