graham23s Posted July 31, 2007 Share Posted July 31, 2007 Hi Guys, im getting a syntax error with an insertion into mysql but i cant see the error anywhere! the query: $query2 = "INSERT INTO `announcements` (`user_id`,`announcement`) VALUES ('$id','$announce') WHERE `user_id`='$id'"; $result2 = mysql_query($query2) or die (mysql_error()); the 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 'WHERE `user_id`='6'' at line 1 done this type of query htousands of times, can nayone see anything wrong? cheers guys Graham Quote Link to comment https://forums.phpfreaks.com/topic/62727-solved-syntax-error/ Share on other sites More sharing options...
realjumper Posted July 31, 2007 Share Posted July 31, 2007 Apostophies perhaps.... $query2 = "INSERT INTO announcements (user_id,announcement) VALUES ($id,$announce) WHERE user_id='$id'"; $result2 = mysql_query($query2) or die (mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/62727-solved-syntax-error/#findComment-312216 Share on other sites More sharing options...
weemikey Posted July 31, 2007 Share Posted July 31, 2007 I'm new to this and that sort of error is the bane of my existence. It's FOR SURE some sort of extra quote or apostrophe. I started by using examples from a book. So I concatenate that sort of a sql statement like this: $query2 = "INSERT INTO announcements (user_id,announcement) VALUES (".$id.",".$announce.") WHERE user_id=".$id; I don't know if that would work, but it gets rid of a few pieces of punctuation. I noticed that your user_id = '6' in the error. Do you mean for the id number to have quotes around it? I just fixed a bug in my code where I was assigning a var using GET like this (note double quotes) : $myvar = $_GET["some_id_number"]. Therefore $myvar = 'some_id_number'. If you do the same assign like this (note single quotes): $myvar = $_GET['some_id_number]. In this example $myvar = some_id_number with NO quotes. If you're searching for an integer that might be an issue as well. Sorry for the rambling post, but I JUST figured that out in my project and I was soooooo happy. So make sure that the $id variable has the right value in it. That alone can add a weird number of extra quotes. Quote Link to comment https://forums.phpfreaks.com/topic/62727-solved-syntax-error/#findComment-312220 Share on other sites More sharing options...
graham23s Posted July 31, 2007 Author Share Posted July 31, 2007 Thanks for the help just appreciate it:) not at all weemikey i love to learn and get new tips from people you would not believe how much you learn per day eh? the other weird thing is when i copies the end of the error into notepad to see exactly what is was showing it showed as: 'WHERE `user_id`='6'' the '' isn't quotations (although it looks like them it's actually 2 ' and ' s both together making them look like quotations lol ultimately it should be: `user_id`='6'" weird ill edit the post if i find out the problem cheers guys Graham Quote Link to comment https://forums.phpfreaks.com/topic/62727-solved-syntax-error/#findComment-312323 Share on other sites More sharing options...
lightningstrike Posted August 1, 2007 Share Posted August 1, 2007 $query2 = "INSERT INTO announcements (user_id,announcement) VALUES ('$id','$announce') WHERE user_id='$id'"; $result2 = mysql_query($query2) or die (mysql_error()); Oops, bumped an old topic. Didn't close this page yesterday Quote Link to comment https://forums.phpfreaks.com/topic/62727-solved-syntax-error/#findComment-312827 Share on other sites More sharing options...
GingerRobot Posted August 1, 2007 Share Posted August 1, 2007 Well, the problem is that you can't have a WHERE clause on an INSERT statement - are you perhaps wanting to UPDATE and existing row? As for quotes here: 'WHERE `user_id`='6'' Thats perefectly right. The error message has the portion of relevant code enclosed in single quotes. The first and last quote are nothing to do with your query. I assume that is what was causing the confusion. Quote Link to comment https://forums.phpfreaks.com/topic/62727-solved-syntax-error/#findComment-312844 Share on other sites More sharing options...
graham23s Posted August 1, 2007 Author Share Posted August 1, 2007 ahh i see you can't put a where clause in an update , i actually can remember someone saying that a while back, how would i go about updating a specific id if i can't use where? cheers Graham Quote Link to comment https://forums.phpfreaks.com/topic/62727-solved-syntax-error/#findComment-312973 Share on other sites More sharing options...
lightningstrike Posted August 1, 2007 Share Posted August 1, 2007 What he means, is you can't use WHERE in an INSERT but UPDATE statements allow where. Quote Link to comment https://forums.phpfreaks.com/topic/62727-solved-syntax-error/#findComment-312985 Share on other sites More sharing options...
graham23s Posted August 1, 2007 Author Share Posted August 1, 2007 ah got ya. thanks guys Graham Quote Link to comment https://forums.phpfreaks.com/topic/62727-solved-syntax-error/#findComment-313018 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.