Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PDO Prepared Statement and Incrementing a column value

Tags:

php

mysql

pdo

I've read this thread: Issues incrementing a field in MySQL/PHP with prepared statements but didn't see the answer to my problem.

PDOStatement Object
(

    [queryString] => UPDATE user_alerts SET notif = ? + 2 WHERE ( user_id = ? )   

)

$stmt->execute( array( 'notif', '1' ) );

As far as I can tell, all this is correct.

When the above code executes, it sets the notif column equal to 2 irregardless of what the value of the notif column is. It's as if the SQL is reading like SET notif = 2 instead of SET notif = notif + 2

I haven't been able to figure it out and would really appreciate help!

like image 295
phpmeh Avatar asked Dec 17 '22 07:12

phpmeh


1 Answers

$sql = 'UPDATE user_alerts SET notif = notif + 2 WHERE ( user_id = :userid )';
$prepStatement = $pdo->prepare( $sql );
$prepStatement->execute(array(':userid' => $userid));

You can't bind a column name to a prepared statement.

like image 198
Bill Avatar answered Feb 24 '23 18:02

Bill