3
votes

I wrote the following code:

 public static function updateUser($userid, $username){
 $query = 'UPDATE users SET username=? WHERE id=?';
    $statement =  $GLOBALS["DB"]->prepare($query);
    $statement->bind_param('is', $userid, $username);
    $statement->execute();
    $statement->store_result();

    if ($statement->affected_rows == 1){
        return  1;
    }
    else{
        return $statement;
    }}

The names of the tables are correct, the data that I pass inside the method too, database is fine, but it fails and returns no error. When I try to return statement data into console it gives me this:

mysqli_stmt Object ( [affected_rows] => 0 [insert_id] => 0 [num_rows] => 0 [param_count] => 2 [field_count] => 0 [errno] => 0 [error] => [error_list] => Array ( ) [sqlstate] => 00000 [id] => 3 ) {"status":"success"}

I checked error list, it is empty. Do I need to set something to make it update?Anyone has an idea what can it be? Database is created like this:

<?php
    require_once ("simplecms-config.php");
    require_once ("./Functions/database.php");

    // Create database connection
    $databaseConnection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    if ($databaseConnection->connect_error)
    {
        die("Database selection failed: " . $databaseConnection->connect_error);
    }
    // Create tables if needed.
    prep_DB_content();
    $GLOBALS['DB'] = $databaseConnection ;
?>
2
Try $statement->bind_param('si', $username, $userid); instead of $statement->bind_param('is', $userid, $username);Hassaan

2 Answers

2
votes

The list of parameters in bind_param are not matching.

Change from

$statement->bind_param('is', $userid, $username);

To

$statement->bind_param('si', $username, $userid);
3
votes

The list of parameters in bind_param needs to match the ones in the query itself.

Since the query has username as the first ? and userid as the second ?, this is what the bind_param call has to use:

$statement->bind_param('si', $username, $userid);