php_beginner_83 Posted July 30, 2009 Share Posted July 30, 2009 Hi All I'm trying to create a form that allows a user to input information into a table in my database. However, the insert keeps failing and I get the message 'Error Insert Failed.'. Can anyone help me understand what's gone wrong. Also I'm not sure if this is a php problem or a MYSQL problem. Thanks for your help. This is my html form.... <html> <head> </head> <body> <form method="post" action="upload.php"> Place Name: <input type="text" name="name"/> <input type="submit"/> </form> </body> </html> and this is my php code... <?php $username = "root"; $password = ""; $hostname = "localhost"; //connection to the database $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); $selected = mysql_select_db("MyWebsite",$dbhandle) or die("Could not select examples"); $result = mysql_query("SELECT * FROM place ORDER BY ID desc limit 1") or die(mysql_error()); $row = mysql_fetch_array($result); $newID = $row['ID'] + 1; mysql_query("INSERT INTO place (ID, Name) VALUES ($newID, $_POST[name]") or die("Error, Insert Failed."); ?> Quote Link to comment https://forums.phpfreaks.com/topic/168181-solved-mysql_query-insert-not-working/ Share on other sites More sharing options...
p2grace Posted July 30, 2009 Share Posted July 30, 2009 You can setup your database to automatically increment your id. Set that up, and then also make sure you always clean your variables before adding to db. Your code should look something like this: <?php $username = "root"; $password = ""; $hostname = "localhost"; if(!isset($_POST['name'])){ die("Name not posted."); } //connection to the database $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); $name = mysql_real_escape_string($_POST['name']); mysql_query("INSERT INTO `place` (`ID`, `Name`) VALUES ('', '$name'") or die("Error, Insert Failed."); ?> Quote Link to comment https://forums.phpfreaks.com/topic/168181-solved-mysql_query-insert-not-working/#findComment-887017 Share on other sites More sharing options...
J.Daniels Posted July 30, 2009 Share Posted July 30, 2009 Also, you can add mysql_error() after trying to INSERT to see if there is an error: mysql_query("INSERT INTO place (ID, Name) VALUES ($newID, $_POST[name]") or die("Error, Insert Failed: ".mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/168181-solved-mysql_query-insert-not-working/#findComment-887110 Share on other sites More sharing options...
mikesta707 Posted July 30, 2009 Share Posted July 30, 2009 Edit: nevermind again, It was a simple syntax error, just not the one I thought try: mysql_query("INSERT INTO place (ID, Name) VALUES ($newID, $_POST[name])") or die("Error, Insert Failed: ".mysql_error()); you are missing a closing ")" character in the query. also, as some people said, you can set up your table to auto increment your id field (IDK if you set up your tables manually, or use something like phpmyadmin, but either way you can pretty easily) in the future, when dealing with sql, it helps to output the mysql error ie query or die("Mysql Error: " . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/168181-solved-mysql_query-insert-not-working/#findComment-887113 Share on other sites More sharing options...
p2grace Posted July 30, 2009 Share Posted July 30, 2009 nevermind, I read your code wrong also, as some people said, you can set up your table to auto increment your id field (IDK if you set up your tables manually, or use something like phpmyadmin, but either way you can pretty easily) in the future, when dealing with sql, it helps to output the mysql error ie query or die("Mysql Error: " . mysql_error()); Displaying the mysql error is definitely necessary for development, just make you remember to turn it off when the site is live or public. Quote Link to comment https://forums.phpfreaks.com/topic/168181-solved-mysql_query-insert-not-working/#findComment-887114 Share on other sites More sharing options...
php_beginner_83 Posted July 31, 2009 Author Share Posted July 31, 2009 Thanks everyone for your input. And the tip about automatically incrementing my ID, I didn't know I could do that with MYSQL. mikesta707 you were right, I was just missing a bracket...arghhh! It's always the easiest thing. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/168181-solved-mysql_query-insert-not-working/#findComment-887591 Share on other sites More sharing options...
p2grace Posted July 31, 2009 Share Posted July 31, 2009 Make sure you clean your $_POST vars before adding them to the database as I had suggested earlier though, it is absolutely necessary. Quote Link to comment https://forums.phpfreaks.com/topic/168181-solved-mysql_query-insert-not-working/#findComment-887613 Share on other sites More sharing options...
php_beginner_83 Posted July 31, 2009 Author Share Posted July 31, 2009 Will do. Thanks for the advice p2grace, as a beginner I appreciate these wee tips. Quote Link to comment https://forums.phpfreaks.com/topic/168181-solved-mysql_query-insert-not-working/#findComment-887627 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.