Jump to content

[SOLVED] Syntax Error


graham23s

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/62727-solved-syntax-error/
Share on other sites

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.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/62727-solved-syntax-error/#findComment-312220
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/62727-solved-syntax-error/#findComment-312323
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/62727-solved-syntax-error/#findComment-312844
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.