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.

...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 you can use reserved words if you quote them correctly, it is best to avoid using them where they can cause problems.

Change $_GET[user] to {$_GET} (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')";

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

-Toxikr3

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.