1

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]'
mnille
  • 1,328
  • 4
  • 16
  • 20
colombo
  • 520
  • 2
  • 9
  • 24
  • This is what I think of as a [Simple to Understand IODKU](http://stackoverflow.com/a/32468519) example that I wrote up. – Drew Jul 12 '16 at 05:02

3 Answers3

6

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.

MrTux
  • 32,350
  • 30
  • 109
  • 146
1

Add primary key constraint on the table:

ALTER TABLE table_name add primary key(col_name)
PHP Geek
  • 3,949
  • 1
  • 16
  • 32
-3

add Unique Key in column name table enter link description here

enter link description here

enter link description here