943,654 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 954
  • PHP RSS
Jul 14th, 2009
0

You have an error in your SQL syntax;

Expand Post »
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:
PHP Syntax (Toggle Plain Text)
  1. $query = ("INSERT INTO friendrequest (username,by) VALUES ('$_GET[user]' , '$by')"); //inserts the request
  2.  
  3. if (!mysql_query($query))
  4. {
  5. die('Error: ' . mysql_error());
  6. }
  7. echo ( "$fusername $by has been sent a request you must now wait for it to be accepted" ); //echos completion
  8. }
  9. else {
  10. echo ( "No request was made" ); // or no request sent
  11. }
  12. }else {
  13. echo ( "You need to be logged in" ); //not logged in
  14. }
Please help, I just can't figure it out. If you require the top code, I will post it.
Last edited by Toxikr3; Jul 14th, 2009 at 11:16 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Toxikr3 is offline Offline
28 posts
since Jul 2009
Jul 15th, 2009
0

Re: You have an error in your SQL syntax;

Click to Expand / Collapse  Quote originally posted by Toxikr3 ...
...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.
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007
Jul 15th, 2009
0

Re: You have an error in your SQL syntax;

Change $_GET[user] to {$_GET['user']} (including the braces). An even better way would be:
php Syntax (Toggle Plain Text)
  1. $user = $_GET['user'];
  2. if(strlen($user) == 0)
  3. {
  4. echo "No user";
  5. die;
  6. }
  7. $query = "INSERT INTO friendrequest (username,by) VALUES ('$user' , '$by')";
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Jul 15th, 2009
0

Re: You have an error in your SQL syntax;

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

-Toxikr3
Reputation Points: 10
Solved Threads: 0
Light Poster
Toxikr3 is offline Offline
28 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: .htaccess (url rewriting)
Next Thread in PHP Forum Timeline: basename($_SERVER["REQUEST_URI"])





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC