Error in SQL syntax

Post here if you need help with SQL.
Post Reply
User avatar
master
Posts: 9
Joined: Wed May 11, 2011 10:16 pm

Error in SQL syntax

Post 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?
------------------------------
TruckingSim - truckingsim.com
CourtRivals - http://www.courtrivals.com/register.php?ref=45143
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Error in SQL syntax

Post by jacek »

its possible that "to" is a mysql keyword, so try wrapping your column and table names in backticks `
Image
JelvinJS7
Posts: 341
Joined: Thu May 12, 2011 8:40 pm

Re: Error in SQL syntax

Post by JelvinJS7 »

"$_SESSION[username]" and "$_POST[message]" should be "$_SESSION['username']" and "$_POST['message']"
you need apostrophes surrounding the phrase in the [square brackets]
User avatar
master
Posts: 9
Joined: Wed May 11, 2011 10:16 pm

Re: Error in SQL syntax

Post 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!
------------------------------
TruckingSim - truckingsim.com
CourtRivals - http://www.courtrivals.com/register.php?ref=45143
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Error in SQL syntax

Post by jacek »

Glad to hear you sorted it :D
Image
libeco
Posts: 104
Joined: Sat May 07, 2011 9:56 am

Re: Error in SQL syntax

Post by libeco »

Post Reply