alan6566 Posted March 1, 2012 Share Posted March 1, 2012 I am trying to insert a new user into my database from my php code. This is the error message that I am getting from the webpage: 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 'order, previousOrder) VALUES ('[email protected]','test','3','callulm','Smith','17' at line 1 This is the code that I am using: <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("deliverpizza", $con); $sql="INSERT INTO customer(userName, password, privilege, firstName, lastName, address, postCode, order, previousOrder) VALUES ('$_POST[username]','$_POST[password]','$_POST[privilege]','$_POST[firstname]','$_POST[lastname]','$_POST[address]','$_POST[postcode]','$_POST[order]','$_POST[previousOrder]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?> Quote Link to comment https://forums.phpfreaks.com/topic/258046-error-code-when-inserting-a-new-user-to-table/ Share on other sites More sharing options...
kojote Posted March 1, 2012 Share Posted March 1, 2012 If there's an error with your SQL query, it might be easier to have the query as created in your php echo'd on your webpage and then copy/paste it and execute it directly in your database, then find the error. I see you use single quotes around your values that have to be putten in a numeric column (like privilege I assume) as well, this could potentially lead to an error. Try keep in mind the value of the variable you try to insert and the value of the column type you want to put it in. Quote Link to comment https://forums.phpfreaks.com/topic/258046-error-code-when-inserting-a-new-user-to-table/#findComment-1322750 Share on other sites More sharing options...
DavidAM Posted March 1, 2012 Share Posted March 1, 2012 Did you read the error message? ... check the manual ... for the right syntax to use near 'order, previousOrder) ... order is a reserved word in mySql. You should avoid using reserved words as column names because it creates this kind of problem. A work-around is to put back-ticks around the reserved word so mySql knows you mean it as a column name: INSERT INTO customer(userName, password, privilege, firstName, lastName, address, postCode, `order`, previousOrder) VALUES Quote Link to comment https://forums.phpfreaks.com/topic/258046-error-code-when-inserting-a-new-user-to-table/#findComment-1322789 Share on other sites More sharing options...
alan6566 Posted March 2, 2012 Author Share Posted March 2, 2012 Thanks that work well thanks. thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/258046-error-code-when-inserting-a-new-user-to-table/#findComment-1323069 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.