cloudll Posted February 19, 2015 Share Posted February 19, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/294734-pdo-prepared-statements-and-incrementing/ Share on other sites More sharing options...
Solution NotionCommotion Posted February 19, 2015 Solution Share Posted February 19, 2015 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)); Quote Link to comment https://forums.phpfreaks.com/topic/294734-pdo-prepared-statements-and-incrementing/#findComment-1506206 Share on other sites More sharing options...
cloudll Posted February 20, 2015 Author Share Posted February 20, 2015 Thanks, all working fine now Quote Link to comment https://forums.phpfreaks.com/topic/294734-pdo-prepared-statements-and-incrementing/#findComment-1506271 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.