Jump to content

pdo prepared statements and incrementing


cloudll

Recommended Posts

Hey everyone,

 

I am pretty new to prepared statements and so far have just been using simple strings.

 

I now would like to increment a table in my database. I have written my code like this:


    require($_SERVER["DOCUMENT_ROOT"] . "/connect.php");

    $pos_x = "pos_x+25";
    $id = "1";

    $sql = "UPDATE movement SET pos_x=? WHERE id=?";
    $statement = $dbh->prepare($sql);
    $statement->execute(array($pos_x,$id));

However when I run it, it just updates the table to 0. Could someone tell me what I have done wrong please?


 

 



 

You only want to use placeholders for the values, not SQL commands.  Try the following:

    require($_SERVER["DOCUMENT_ROOT"] . "/connect.php");

    $pos_x = 25;
    $id = "1";

    $sql = "UPDATE movement SET pos_x=pos_x+? WHERE id=?";
    $statement = $dbh->prepare($sql);
    $statement->execute(array($pos_x,$id));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.