xProteuSx Posted December 15, 2008 Share Posted December 15, 2008 I cannot get this MySQL query to work. My code is as follows: $id = mysql_real_escape_string($_GET['id']); //does return the proper value when I use echo command to display $query = "INSERT INTO rb_notes(image_thumb) VALUES ('$thumbnail') WHERE ID LIKE $id"; Error I get is as follows: Error in query : 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 ID LIKE' at line 1 I have tried every variation that I can think of: $query = "INSERT INTO rb_notes(image_thumb) VALUES ('$thumbnail') WHERE ID LIKE '$id'"; $query = "INSERT INTO rb_notes WHERE ID LIKE '$id' (image_thumb) VALUES ('$thumbnail')"; $query = "UPDATE rb_notes(image_thumb) VALUES ('$thumbnail') WHERE ID LIKE '$id'"; etc etc ... Please help! Quote Link to comment https://forums.phpfreaks.com/topic/136985-solved-mysql-query-problem/ Share on other sites More sharing options...
Yesideez Posted December 15, 2008 Share Posted December 15, 2008 You can't use WHERE with INSERT. $query = "INSERT INTO rb_notes (image_thumb) VALUES ('$thumbnail')"; Try that. You also had no space bwtween rb_notes and (image_thumb). Quote Link to comment https://forums.phpfreaks.com/topic/136985-solved-mysql-query-problem/#findComment-715485 Share on other sites More sharing options...
xProteuSx Posted December 15, 2008 Author Share Posted December 15, 2008 Yesideez, Then I guess that I have to use update instead of insert, because I have to manipulate certain rows only (as in id=6 or whatever). At the moment I am trying this, but am having no luck: $query = "UPDATE SET rb_notes image_thumb='$thumbnail' WHERE id_notes LIKE='$id'"; Error in query : 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 'SET rb_notes image_thumb='/uploads/thumb/200.jpg' WHERE id_notes LIKE=''' at line 1 Really strange, considering that if I do this: echo $id; I have a value returned to me. Quote Link to comment https://forums.phpfreaks.com/topic/136985-solved-mysql-query-problem/#findComment-715493 Share on other sites More sharing options...
Yesideez Posted December 15, 2008 Share Posted December 15, 2008 When you use LIKE you don't use equals: $query = "UPDATE SET rb_notes image_thumb='$thumbnail' WHERE id_notes LIKE '$id'"; Quote Link to comment https://forums.phpfreaks.com/topic/136985-solved-mysql-query-problem/#findComment-715495 Share on other sites More sharing options...
DarkWater Posted December 15, 2008 Share Posted December 15, 2008 Your syntax is completely messed up. Go read up on SQL syntax. Here is what your query SHOULD look like: $query = sprintf("UPDATE rb_notes SET image_thumb = '%s' WHERE id_notes = %s", $thumbnail, (int) $id); Quote Link to comment https://forums.phpfreaks.com/topic/136985-solved-mysql-query-problem/#findComment-715496 Share on other sites More sharing options...
Yesideez Posted December 15, 2008 Share Posted December 15, 2008 btw... Error in query : 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 'SET rb_notes SET image_thumb='/uploads/thumb/200.jpg' WHERE id_notes LIKE=''' at line 1 Seems like $id isn't being populated! You're passing an empty variable to the query. When changing anything in the database and you're relying on something to identify the rows targeted for modification, it's always best to check first that your conditional data contains exactly what you intend it to otherwise you can change more than you planned to! Quote Link to comment https://forums.phpfreaks.com/topic/136985-solved-mysql-query-problem/#findComment-715498 Share on other sites More sharing options...
xProteuSx Posted December 15, 2008 Author Share Posted December 15, 2008 Yesideez, I know what you are saying. I fixed up the query, and I printed the value of $id one line before the query, and it is not NULL. I really don't know what i going on. DarkWater, I have NEVER seen mysql code being executed like that. Mind you, my PHP texts are from the late nineties, I think. Can you recommend a good site on SQL syntax in the way you have shown it? I haven't even come across that sort of code on the internet, and God knows I have done many searches and read a few articles. Quote Link to comment https://forums.phpfreaks.com/topic/136985-solved-mysql-query-problem/#findComment-715510 Share on other sites More sharing options...
DarkWater Posted December 15, 2008 Share Posted December 15, 2008 sprintf() doesn't actually execute the query, it just lets you create a format string and it helps when you're making queries IMO. Look sprintf() in the manual if you're really curious. The syntactic part of the query is the SQL-standard way of doing an UPDATE. I think you should get a new PHP book, because PHP is so unlike how it used to be that anything older than 5 years is considered extremely outdated in my book. Quote Link to comment https://forums.phpfreaks.com/topic/136985-solved-mysql-query-problem/#findComment-715519 Share on other sites More sharing options...
xProteuSx Posted December 16, 2008 Author Share Posted December 16, 2008 In the end, this worked for me: $query = "UPDATE rb_notes SET image_thumb='$thumbpath' WHERE id_notes='$idthumb'"; Quote Link to comment https://forums.phpfreaks.com/topic/136985-solved-mysql-query-problem/#findComment-716316 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.