phpbeginner22 Posted June 10, 2009 Share Posted June 10, 2009 I am creating a registration form for my website, and I keep getting an insert error when I submit the registration form. The registration page is very simple it assigns a user a random customer key, a username and a password. I have used this same insert syntax in other forms before, but for some reason I keep getting an error here. Help? Error: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key,username,password) VALUES ('1559802062','name','password')' at line 1 Code: <?php include 'connect.php'; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $key=$_POST[key]; $sql="INSERT INTO logins (key, username, password) VALUES ('$key','$_POST[username]','$_POST[password]')"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } echo "You have successfully registered!"; ?> I have checked the syntax so many times and I can't find this illusive error. Anyone have suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/161690-insert-error-beginner-needs-help/ Share on other sites More sharing options...
cunoodle2 Posted June 10, 2009 Share Posted June 10, 2009 no quotes around the $key. its an integer...not a string. Quote Link to comment https://forums.phpfreaks.com/topic/161690-insert-error-beginner-needs-help/#findComment-853149 Share on other sites More sharing options...
haku Posted June 10, 2009 Share Posted June 10, 2009 quotes around integers are ok. Change this: die('Error: ' . mysql_error()); to this: die('Error: ' . mysql_error() . '<br />' . $sql); and tell us what it says. And please use code tags in the future. Quote Link to comment https://forums.phpfreaks.com/topic/161690-insert-error-beginner-needs-help/#findComment-853154 Share on other sites More sharing options...
Maq Posted June 10, 2009 Share Posted June 10, 2009 'key' is a reserved word you need backticks around it in your query. Quote Link to comment https://forums.phpfreaks.com/topic/161690-insert-error-beginner-needs-help/#findComment-853155 Share on other sites More sharing options...
phpbeginner22 Posted June 10, 2009 Author Share Posted June 10, 2009 I changed the term for "key" to customercode and added the new die syntax and it worked perfectly. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/161690-insert-error-beginner-needs-help/#findComment-853162 Share on other sites More sharing options...
Maq Posted June 10, 2009 Share Posted June 10, 2009 I changed the term for "key" to customercode and added the new die syntax and it worked perfectly. Thanks guys! Good. You could have still kept the same field name but just put backticks around it. $sql="INSERT INTO logins (`key`, username, password) You should take out the or die clause when putting this on your production server. Quote Link to comment https://forums.phpfreaks.com/topic/161690-insert-error-beginner-needs-help/#findComment-853166 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.