I’m trying to update a field in my database with 1 each time the statement is true. I’m unsure how to do this. With my following code I’m not sure about this issues (although a lot of googling):
- How to add 1 the existing fields value (vote = vote + 1)
- The where statement combined with the bindParam.
I dont get any error message, although it doesn’t work. What am I doing wrong?
My code look like this (the $id variable is correct have printed out this and it has the “right” value, $dbh is also set with the right connect):
$stmt = $dbh->prepare("UPDATE rating SET vote = vote+1 WHERE rel_id_product = ':id'");
$stmt->bindParam(':id', $id, PDO::PARAM_STR);
$stmt->execute();
Don’t quote the place holders. PDO will do that for you if necessary when it comes time to “insert” the value you’re using in the query:
Also, do
var_dump($stmt->execute)and see what you get. if it’s a boolean false, then the query has failed. You’re also assuming the prepare call has succeeded and try to execute it regardless. Never assume a DB operation has suceeded. Even if the SQL syntax is perfect, there’s far too many other reasons for things to blow up to not check for that.