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? Link to comment https://forums.phpfreaks.com/topic/294734-pdo-prepared-statements-and-incrementing/ Share on other sites More sharing options...
NotionCommotion Posted February 19, 2015 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)); 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 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
Archived
This topic is now archived and is closed to further replies.