Using PDO Prepared Statement and Incrementing a column value

后端 未结 2 1111
情书的邮戳
情书的邮戳 2021-01-13 19:06

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
(

          


        
相关标签:
2条回答
  • 2021-01-13 19:25

    Using parameters is not just a simple text replacement. You can't replace a column name with a parameter. MySQL will interpret your query as if you had written this:

    UPDATE user_alerts SET notif = 'notif' + 2 WHERE ( user_id = ? ) 
    

    The string 'notif' is converted to zero for the addition.

    Try this query instead:

    UPDATE user_alerts SET notif = notif + 2 WHERE ( user_id = ? )   
    
    0 讨论(0)
  • 2021-01-13 19:28
    $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.

    0 讨论(0)
提交回复
热议问题