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,320
  • 4
  • 15
  • 19
colombo
  • 500
  • 9
  • 21
  • 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

5

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
  • 30,335
  • 25
  • 102
  • 137
1

Add primary key constraint on the table:

ALTER TABLE table_name add primary key(col_name)
PHP Geek
  • 3,810
  • 1
  • 12
  • 27
-2

add Unique Key in column name table enter link description here

enter link description here

enter link description here