views:

90

answers:

4

I am trying to use an update query to change values held in a database, the new values I am trying to store are held in php variables. I'm not sure what it is I'm doing wrong.

mysql_query("UPDATE user SET status='full' WHERE user_id = '$user_id'")or die(mysql_error());

here is the error message

Duplicate entry '91317691' for key 1

thanks in advance

A: 

I think you need to change

... '" + $user_id + "'" ...

put closing double quote " after your single, then add your $user_id, then add the final closing "'" (double quote, single quote, double quote )

Additionally, if you don't have control of the $user_id variable, you could be open to SQL-injection attacks

DRapp
A: 

It looks like your user_id field is an integer. Putting '' around it will break things

try it without the quotes

mysql_query("UPDATE user SET status='full' WHERE user_id = $user_id")or die(mysql_error());
Galen
MySQL automagically casts something like `'12'` to an integer if necessary.
VolkerK
its never done that for me, always messed up my query
Galen
see http://dev.mysql.com/doc/refman/5.0/en/type-conversion.html
VolkerK
+1  A: 

It means that there is a duplication in unique key. Am supposing you are using unique on (user_id, status) and status is of type enum, if that the case, you might already be having an entry matching the one you are trying to update.

| user_id | status |
| 1234    | full   |
| 1234    | none   |

if thats not the case, posting schema would be helpful.

Kalyan
A: 

apologies, I realized the problem was caused elsewhere in my php script, but I have resolved it.

thanks for your guidance

bell