MySQL Insert Into

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Plxply
Forum Newbie
Posts: 14
Joined: Sat Dec 19, 2009 2:40 pm

MySQL Insert Into

Post 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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: MySQL Insert Into

Post by daedalus__ »

well what does the error message say?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: MySQL Insert Into

Post 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]
Plxply
Forum Newbie
Posts: 14
Joined: Sat Dec 19, 2009 2:40 pm

Re: MySQL Insert Into

Post 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.
Post Reply