Page 1 of 1

MySQL Insert Into

Posted: Sat Dec 19, 2009 2:42 pm
by Plxply
Hello,

This may be one of the most easiest questions you have received, although I have only just started to begin learning PHP and have decided to try and use a MySQL database.

I am currently experiencing issues with the following SQL command:

Code: Select all

 
    $sql="INSERT INTO Mail (From,To,Subject,Message,IP,DateTime)
    VALUES ('$from','$to','$subject','$message','$ip','$date')";
 
The table and all appropriate fields are created, all of the variables are correctly defined and the connection to the database is successful but when ever this command is run I receive an SQL syntax error. Any help that you could provide would be greatly appreciated.

Re: MySQL Insert Into

Posted: Sat Dec 19, 2009 4:23 pm
by daedalus__
well what does the error message say?

Re: MySQL Insert Into

Posted: Sat Dec 19, 2009 4:35 pm
by Eran
FROM is a reserved word in MySQL, you need to escape it with backticks to use it as field -
http://dev.mysql.com/doc/refman/5.1/en/ ... words.html
It's good practice to always escape field and table names with backticks -
[sql]INSERT INTO `Mail` (`From`,`To`,`Subject`,`Message`,`IP`,`DateTime`) VALUES (...)[/sql]

Re: MySQL Insert Into

Posted: Sat Dec 19, 2009 5:29 pm
by Plxply
Thank you ever so much pytrin, I would have never figured this out on my own as I had already been checking Google thinking there was a problem somewhere else.