Problem in Insert with On Duplicate Key Update

Hi…

I have code for importing data from .xml file to database.

My issue is the On Dupliicate Key Update did not work, ti save again the data even the data is already exist…

here is my code:


$sql = "INSERT INTO sales_order (ProductType,WorkOrder,POIssueDate,SalesMonth)
      VALUES
      ('$ProductType','$WorkOrder','$POIssueDate','$SalesMonth')
      ON DUPLICATE KEY UPDATE
      ProductType = '$ProductType', WorkOrder = '$WorkOrder', POIssueDate = '$POIssueDate', SalesMonth = '$SalesMonth'" or die(mysql_error());

For example I attach file then I attahc again but I update some data I want it to update on database if the ProductType is already exist and add if not.

I attach my sample upload file. and You can see on the second upload the ProductType P101 change the sales month.

In my query the data that was second upload was insert again it did not update the existing data.I want the result should be the second upload not mix the first and second upload. Now the data was duplicated, thats my problem. I want it to update if the ProductType is already exist.

Thank you

please do a SHOW CREATE TABLE for this table

I think you are missing a key in your query.

[table=“width: 100”]
[tr]
[td]Key[/td]
[td]Name[/td]
[/tr]
[tr]
[td]1[/td]
[td]Test[/td]
[/tr]
[tr]
[td]2[/td]
[td]Test 2[/td]
[/tr]
[tr]
[td]3[/td]
[td]Test 3[/td]
[/tr]
[/table]

$sql = “INSERT INTO tabe (Key, Name)
VALUES
($key, $name)
ON DUPLICATE KEY UPDATE
Name = $name” or die(mysql_error());

This will update record 1

$sql = “INSERT INTO tabe (Key, Name)
VALUES
(1, ‘Test update’)
ON DUPLICATE KEY UPDATE
Name = ‘Test update’” or die(mysql_error());

This will insert new record. Key doesn’t have to be filled to insert new record if you made it auto increment.

$sql = “INSERT INTO tabe (Key, Name)
VALUES
(4, ‘Test new record’)
ON DUPLICATE KEY UPDATE
Name = ‘Testupdate’” or die(mysql_error());