Page 1 of 1

Error in SQL syntax

Posted: Fri May 13, 2011 12:41 am
by master
I am a beginner when it comes to PHP so please bare with me.
$query = mysql_query("SELECT * FROM send_pm WHERE to = '$_SESSION[username]'") or die (mysql_error());
Now for some reason I keep getting this 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 = 'test'' at line 1
$insert = mysql_query("INSERT INTO send_pm (to, subject, message) VALUES ('$_POST[to]', '$_POST[subject]', '$_POST[message]')") or die (mysql_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, subject, message) VALUES ('test', 'test', 'test')' at line 1

Now, I've looked it over and to me it seems right. I've done both of those plenty of times before and they worked flawlessly but for some reason they don't want to work now.

What's wrong with it?

Re: Error in SQL syntax

Posted: Fri May 13, 2011 1:01 am
by jacek
its possible that "to" is a mysql keyword, so try wrapping your column and table names in backticks `

Re: Error in SQL syntax

Posted: Fri May 13, 2011 1:02 am
by JelvinJS7
"$_SESSION[username]" and "$_POST[message]" should be "$_SESSION['username']" and "$_POST['message']"
you need apostrophes surrounding the phrase in the [square brackets]

Re: Error in SQL syntax

Posted: Sat May 14, 2011 9:40 pm
by master
jacek wrote:its possible that "to" is a mysql keyword, so try wrapping your column and table names in backticks `
Sorry, it took me so long to respond and yes "to" was apparently the problem. Thanks!

Re: Error in SQL syntax

Posted: Sat May 14, 2011 9:44 pm
by jacek
Glad to hear you sorted it :D

Re: Error in SQL syntax

Posted: Sun May 15, 2011 4:41 pm
by libeco