-2

I am making a game for class and I have a user settings page where the admin can modify the settings.

When I click the update button I am returned with this error:

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

This is the code where I believe the error is originating:

<?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();
    }
}

?>

This is my db connection code in 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.');
}
?>

Does anyone know why I am getting this error?

Marcel Gwerder
  • 8,353
  • 5
  • 35
  • 60
user3481788
  • 161
  • 10
  • because `$insert` is not a statement, is this all of your code? where is your db connection did you skip to post that or you didn't connect to db? – user2009750 Apr 12 '14 at 13:18
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Anonymous Apr 12 '14 at 13:18
  • 1
    Does this type of `UPDATE` syntax even exist? Never seen it before... looks more like insert to me except for the `UPDATE` keyword. – Marcel Gwerder Apr 12 '14 at 13:20
  • @MarcelGwerder that may be the problem. I'll check... – user3481788 Apr 12 '14 at 13:20
  • 1
    your `prepare` didn't work, should be an error in your update statement – CodeBird Apr 12 '14 at 13:20

1 Answers1

2

Your update statement should probably be like:

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

Usually in an update it also makes sense to have some WHERE clause, but I don't know what exactly your use case is.

Marcel Gwerder
  • 8,353
  • 5
  • 35
  • 60
  • I would have a WHERE clause but I didn't have any need for an ID in this table as the settings table won't change. – user3481788 Apr 12 '14 at 13:32