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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.