Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO prepared statement trouble

I am currently trying to run a query where the current value of a mysql table column increase itself by 1... Let me show this with mysql query example

$sql = mysql_query("UPDATE `table` SET quantity=quantity+1 WHERE id='$id'");

I am unable to do this in PDO prepared statement...

$sql = "UPDATE `table` SET quantity=:quants+1 WHERE id=:userid";
$sql_prep = $db->prepare($sql);
$sql_prep->bindParam(":quants", what will i write here??);
$sql_prep->bindParam(":userid", $id);
$sql_prep->execute();

Help needed..! Thanks

like image 821
nick Avatar asked Oct 20 '22 13:10

nick


1 Answers

You don't need to pass that as a parameter, just do:

$sql = "UPDATE `table` SET quantity=quantity+1 WHERE id=:userid";
$sql_prep = $db->prepare($sql);
$sql_prep->bindParam(":userid", $id);
$sql_prep->execute();
like image 128
The Blue Dog Avatar answered Oct 27 '22 10:10

The Blue Dog