m7md m7md - 4 days ago 7
MySQL Question

On duplicate key update not working

I try to use on duplicate key update in my query but it still duplicate some value.

My query is like this:

$insert=mysqli_query($conn,"insert into tbl_staffdistribution(db_user,db_name,db_responsible,db_date)values('$user','$name','$responsible','$formatteddatetimein') on duplicate key update db_user='$user',db_name='$name',db_responsible='$responsible',db_date='$formatteddatetimein'")or die(mysqli_error($conn));


I have db_id in my database is a primary key auto increment.

The problem is that I have a duplicate value in my database and that should not happen How can I solve this problem ??

Answer

In order to do this. you should be having a unique key within your table so that you can use it for on duplicate key update as such. The unique key can be your primary or some other field from your table. You can do something like this within your query:

ON DUPLICATE KEY UPDATE someID = VALUES(someID)

A clear cut explanation from MySQL.

Comments