After SQL update, field is set to 0 [PHP/MySQL]
Okay, I'm currently sick so maybe the answer is right in front of me but I've been sitting here for the past 4 hours trying to figure out why this is happening.
Basically I have a script that is supposed to update a row in the database. The field I'm trying to update is 'name' and it looks like this:
// Updating the database with the new name
$name = $this->db->slashes( $this->friends[$row->key] );
$sql = "UPDATE `friends` SET `name` = '" . $name . "' AND `status` = 'updated' WHERE `key` = " . $row->key . " LIMIT 1";
echo '-- Query: ' . $sql . '<br />';
$result2 = $this->db->query( $sql );
if( !$result2 )
echo mysql_error();
Output becomes this(an example):
-- Query: UPDATE `friends` SET `name` = 'Andrew Johnsson' AND `status` = 'updated' WHERE `key` = 7823583 LIMIT 1
It did not generate any mysql_error so the query seem to have gone through properly and by the looks of the query, it should have just updated a row in the database where the key was a number and set the new name to Andrew Johnsson aswell as setting status to updated.
However! Aft开发者_开发技巧er looking in the database after this update, the name for this row is set to '0'. Why on earth did it do that?
Any ideas as to why this is happening? Also, tell me if you need some more information to be able to help me and I'll kindly provide it!
Your UPDATE syntax is incorrect. It should be SET name = 'string', status = 'updated' WHERE key...
Currently you are literally asking it to evaluate the AND's in order to determine what the name column should be updated with, which is why you're getting the 0.
Well, this might be too silly, but name
is a string field, right? If it's set to any kind of integer field, this is exactly what would happen: the string is parsed as 0
.
I bet it's something a wee bit more complicated than that, though.
精彩评论