Jump to content

pdo prepared statements and incrementing


cloudll
Go to solution Solved by NotionCommotion,

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?


 

 



 

Link to comment
Share on other sites

  • Solution

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));
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.