change a field in mysql table?
i want all fields that got a certain value to be changed to a different value.
but my code gave me an error
UPDATE maps
SET city_id = $new_id
WHERE city_id = $old_id
you cant write it like that?
I get thi开发者_StackOverflow社区s error code:
Couldn't execute 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 '' at line 3
$query = "UPDATE maps
SET city_id = '$new_id'
WHERE city_id = '$old_id'";
Additional example here
If your IDs are integers, make sure that there's none that are null, since the command
UPDATE maps SET city_id=4 WHERE city_id=
will fail, while
UPDATE maps SET city_id='4' WHERE city_id=''
will not fail, though likely won't update anything, presuming city_id
is the primary key of your table and all rows are supposed to have one, so none will have city_id
equal to an empty string.
If you are still getting an error, have your script echo out the full SQL string (after variables have been replaced with values) to see if there's unusual input in your variables causing it.
精彩评论