Paul Dessert
Paul Dessert

Reputation: 6389

ON DUPLICATE KEY creating new records

I'm getting a bunch of product info from the Amazon API. I'm using the ASIN as my primary key. When running an INSERT I'd like to check if it exists, if so, update a few values and move on. I don't want duplicate records.

Here is a code snippet:

$stmt = $this->dbh->prepare("
        INSERT INTO products(ASIN, Many, Other, Values)
        VALUES(:ASIN, :Many, :Other, :Values)
        ON DUPLICATE KEY UPDATE Other= :Other
        ")

$stmt->execute(array(
                        ':ASIN'     =>  $ASIN,
                        ':Many'     =>  $many,
                        ':Other'    =>  $other,
                        ':Values'   =>  $values
               ));

This simply adds duplicate records to my db. How can I check the ASIN and if it's a duplicate, update it with any new info?

Upvotes: 1

Views: 51

Answers (1)

cjhill
cjhill

Reputation: 1064

You need to set the keys in your database as unique or primary keys, otherwise the engine will not know if it is a duplicate key or not.

Upvotes: 1

Related Questions