user2509541
user2509541

Reputation: 268

PDO Exception when binding parameters in an UPDATE query

I have the following SQL UPDATE query using PDO and binding two parameters, but I get an error:

$updateSql = $con->prepare("UPDATE menu SET '".$field."' = :value WHERE 'id' = :idField");
$updateSql->execute(array(
    ':value' => $value,
    ':idField' => $id));

And the error is telling me:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 Syntax error next to ''dish' = ? WHERE 'id' = ?' 

NOTE: The UPDATE is executed correctly, but the script stops running with the Fatal Error.

Upvotes: 1

Views: 289

Answers (1)

MH2K9
MH2K9

Reputation: 12039

You enclosed the column name by single quotes. Remove single quotes.

$updateSql = $con->prepare("UPDATE menu SET ".$field." = :value WHERE id = :idField");

Upvotes: 1

Related Questions