0

Can someone tell me where I'm wrong with my request?

$sql = "INSERT INTO order (order_id,prod) VALUES ('','".$prod."')";
mysql_query($sql) or die ('Error SQL !'.$sql.'<br />'.mysql_error());
$_SESSION['orderid']=mysql_insert_id();

Here is my table configuration:

Columns for order table:

'order_id'=>int(11) auto_increment
'prod'=> varchar(20) utf8_general_ci

And this is the error message:

Error SQL !INSERT INTO order (order_id,prod) VALUES ('','xxx') 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 (order_id,prod) VALUES ('','xxx')' at line 1

Thank you

3
  • Please look into PDO or MySQLi for database handling. MySQL is deprecated now Commented Jun 30, 2014 at 11:42
  • Also, posting the value of $prod will help :) Commented Jun 30, 2014 at 11:42
  • it is posted in the error message (that is: xxx) Commented Jun 30, 2014 at 11:43

4 Answers 4

2

ORDER is a reserved keyword and happens to be the name of your column/table. To avoid syntax error, you need to escape it using backtick. E.g.

`ORDER`

If you have the privilege to alter the table, change the table name to which is not a reserved keyword to avoid problem from occurring again.

Waring: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

INSERT INTO `order` (order_id,prod) VALUES ('','".$prod."')

Comments

1

replace with this, just use single quote with table name

$sql = "INSERT INTO `order` (order_id,prod) VALUES ('','".$prod."')";

Comments

0

$insert="INSERT INTO order (order_id,prod) VALUES ('','".$prod."')";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.