Jump to content

Error code when inserting a new user to table


alan6566

Recommended Posts

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 ('c_s@gmail.com','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)
?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.