glassfish Posted November 25, 2014 Share Posted November 25, 2014 (edited) This UPDATE script works: $query = $pdo->prepare("UPDATE posts SET headline = ? WHERE id = ?"); $query->bindValue("1", $_POST['headline']); $query->bindValue("2", "2"); $query->execute(); Though, this INSERT script does not work: $query = $pdo->prepare("INSERT INTO posts (headline, post) VALUES (?, ?)"); $query->bindValue("1", $_POST['headline']); $query->bindValue("2", $_POST['post']); $query->execute(); Nothing gets added, when executing this script. The connection: <?php $dsn = "mysql:dbname=phpblog;host=localhost"; $user = "root"; $password = ""; try{ $pdo = new PDO($dsn, $user, $password); }catch(PDOException $e){ echo "Connection failed: " . $e->getMessage(); } ?> Any suggestions why the INSERT script is not working? Edited November 25, 2014 by glassfish Quote Link to comment https://forums.phpfreaks.com/topic/292703-pdo-inserting-data/ Share on other sites More sharing options...
ginerjm Posted November 25, 2014 Share Posted November 25, 2014 Your insert doesn't reference the id column. Is that column an auto-increment one? Otherwise I think you need to specify all the columns when doing an insert. Quote Link to comment https://forums.phpfreaks.com/topic/292703-pdo-inserting-data/#findComment-1497627 Share on other sites More sharing options...
mac_gyver Posted November 25, 2014 Share Posted November 25, 2014 i'm going to guess you don't have a column in your table named post and that the prepare() statement is failing with an error, assuming that emulated prepares are turned off, or the execute() statement is failing with an error, assuming that emulated prepared are not turned off (which unfortunately is the default.) well written code is self-troubleshooting. it will tell you when, where, and why it is failing. to get your code to tell you when, where, and why it is failing, your code must have error checking logic in it. you can either add conditional logic to test the result of each statement to find out if it is working or not or you can use exceptions. i recommend using exceptions for fatal problems. Quote Link to comment https://forums.phpfreaks.com/topic/292703-pdo-inserting-data/#findComment-1497628 Share on other sites More sharing options...
Solution glassfish Posted November 25, 2014 Author Solution Share Posted November 25, 2014 Thanks for the responses. I have had mispelled the column "post" into "posts" in MySQL. After correcting the spelling mistake it is working now. Quote Link to comment https://forums.phpfreaks.com/topic/292703-pdo-inserting-data/#findComment-1497632 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.