0

I'm having a problem inserting some data to a mysql database. I have used the same method with other features on the site, and this is the only one causing problems. It's meant to input into 3 field in the database (To, From, Message). As you can see it's a very basic messaging system.

I have the data coming into PHP via AJAX. But the problem is within the INSERT. I have messed around with it for over an hour now - no luck! Here is the code to insert:

mysql_query("INSERT INTO messages (To, From, Message) VALUES('$to','$loggedin','$message') ") 
or die(mysql_error());

And here is the SQL syntax 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 'To, From, Message) VALUES('Ryan','Ryan','hhh')' at line 1

I have tried adjusting a lot of things, no luck! :(

1 Answer 1

3

"TO" and "FROM" are reserved keywords, it's not wise to use them as column names. You have to escape them with a back-tick "`". Try this:

INSERT INTO messages (`To`, `From`, `Message`)

See the list with reserved words: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

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

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.