954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

You have an error in your SQL syntax;

Hi guys, I was trying to implement a friends script but I encountered this error


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 'by) VALUES ('Toxikr3' , 'moderator')' at line 1

I tried to insert values Toxikr3 and moderator in the columns username and by

It inserts fine when there is only one thing to insert, but gives me error when I insert more then one.

My Code:

$query = ("INSERT INTO friendrequest (username,by) VALUES ('$_GET[user]' , '$by')"); //inserts the request

if (!mysql_query($query))
  {
  die('Error: ' . mysql_error());
  }
echo ( "$fusername $by has been sent a request you must now wait for it to be accepted" ); //echos completion 
 }
else { 
echo ( "No request was made" ); // or no request sent 
} 
}else { 
echo ( "You need to be logged in" ); //not logged in 
}

Please help, I just can't figure it out. If you require the top code, I will post it.

Toxikr3
Light Poster
28 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 
...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 'by) VALUES ('Toxikr3' , 'moderator')' at line 1

Re-read the quoted part above, then read the table of reserved words in the MySQL manual. (Hint: 'by' is a reserved word.)

While youcan use reserved words if you quote them correctly, it is best to avoid using them where they can cause problems.

Fest3er
Posting Whiz in Training
242 posts since Aug 2007
Reputation Points: 51
Solved Threads: 35
 

Change $_GET[user] to {$_GET['user']} (including the braces). An even better way would be:

$user = $_GET['user'];
if(strlen($user) == 0)
{
   echo "No user";
   die;
}
$query = "INSERT INTO friendrequest (username,by) VALUES ('$user' , '$by')";
darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

Thank you for your replys, I will try them out and hope it works.

-Toxikr3

Toxikr3
Light Poster
28 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You