Prepared SQL Statement in PHP Not Updating MySQL Database Row

时间:2015-10-30 21:59:32

标签: php mysql sql database prepared-statement

I am attempting to update a MySQL database table with new rows from a PHP script. The script is called from a frontend HTML form that gets seralised and passed to the PHP as $_POST variables.

    $stmt = $con->prepare("UPDATE blog SET tag = ?, datestamp = ?, title = ?, content = ?, views = ?, shares = ? WHERE id=?");
    $stmt->bind_param("ssssiii", $tag, $datestamp, $title, $content, $views, $shares, $postid);
    $stmt->execute();

    / Check whether the execute() succeeded 
    if ($stmt->errno) {
        echo "FAILURE! " . $stmt->error;
    }
    else {
        echo var_dump($stmt);
        printf("%d Row updated.\n", $stmt->affected_rows); 
    }

The request does not throw am error, but the database row does not get updated, and it outputs "0 Rows updated". The serialised data is being sent as the right types (strings and ints where appropriate). Does anyone know what might be causing the issue?

echo var_dump($stmt) returns :

object(mysqli_stmt)#2 (9) { ["affected_rows"]=> int(0) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(7) ["field_count"]=> int(0) ["errno"]=> int(0) ["error"]=> string(0) "" ["sqlstate"]=> string(5) "00000" ["id"]=> int(1) }

1 个答案:

答案 0 :(得分:1)

Your code looks fine, double check what is $postid and if that matches a record with id in blog table.

Also make sure you are using correct database (check the connection details).

Side note: do not use echo var_dump(), var_dump() itself will echo it.