Here is the scenario: I need a mechanism to update a value (adding 1 to the current value) which is stored in a database for a certain user who logs in to the website. This is the current code:
$value = $row["value"];
$add_value = $db->prepare('UPDATE table SET value = $value + 1 WHERE email = :email');
$add_value->execute(array(':email' => $email));
But what I receive is the following error message:
Parse error: syntax error, unexpected T_LNUMBER in …
What am I doing wrong?
First, you’re using single quotes, that means that variables will not be parsed. (The query is literally seeing
$valueand not the actual value).Second, you don’t need to know the value in advance, the following works fine:
The database engine knows to update the field appropriately.