colombo
colombo

Reputation: 520

Mysql on duplicate key update is not working

I am trying to use following code segment to update the database when inserting duplicates. But instead of updating it still inserting duplicate rows. Why?

$import = "INSERT INTO data(Product,Courier,Received_Date,Acc_No,Received_By,Delivered_Date,Month,Year,Bill_Run,Dispatch_Type,Status,Bounce_Code) values('$data[0]','$data[1]','$Received_Date','$data[3]','$data[4]','$Delivered_Date','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]') ON DUPLICATE KEY UPDATE Acc_No = '$data[3]'

Upvotes: 1

Views: 8893

Answers (3)

nirav goriya
nirav goriya

Reputation: 1

add Unique Key in column name table enter link description here

enter link description here

enter link description here

Upvotes: -3

PHP Geek
PHP Geek

Reputation: 4033

Add primary key constraint on the table:

ALTER TABLE table_name add primary key(col_name)

Upvotes: 1

MrTux
MrTux

Reputation: 33993

For 'ON DUPLICATE KEY UPDATE' to work you need a unique or primary key constraint on a table. Only if you would get a key conflict on inserting the 'ON DUPLICATE KEY UPDATE ' code is executed.

Upvotes: 7

Related Questions