0

I wrote this code:

mysql_query("INSERT INTO test (user_id, word_id, right) VALUES ('6', '23', '5' )") or die(mysql_error()); 

But an error message prompts:

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 'right) VALUES ('6', '23', '5' )' at line 1

What I did wrong?

1
  • 2
    You should not use character literals for numbers. '6' is a character, 6 is a number. Commented Dec 10, 2012 at 11:22

4 Answers 4

4

right is a reserved word , escape it like so:

INSERT INTO test (user_id, word_id, `right`) VALUES ('6', '23', '5' )

Note that:1

Use of this extension(mysql_query) is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

mysqli_query()
PDO::query()

1:quoted from PHP Manual: mysql_query

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

1 Comment

Thanks a lot! What are the other reserved words?
2

right is reserved key word 9.3. Reserved Words when we use reserved keyword we need to escape them try

mysql_query("INSERT INTO test (user_id, word_id, `right`) VALUES ('6', '23', '5' )") or die(mysql_error()); 

enter image description here

Comments

0

Try this :::RIGHT is known keyword, try a backtick

INSERT INTO test (`user_id`, `word_id`, `right`) VALUES ('6', '23', '5' )

Comments

0
INSERT INTO `test` (`user_id`, `word_id`, `right`) VALUES ('6', '23', '5' )

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.