m7md m7md - 1 year ago 79
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 Source

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:


A clear cut explanation from MySQL.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download