MySQL Update on Duplicate entries for Inserts
Particletree has a great article on a MySQL query that updates a record in a database when trying to insert data that has a record with a duplicate key. Check out the code below.
Old Way
PHP:
-
$sql = 'SELECT TransId FROM Sales WHERE TransId = 123';
-
$rs = $db->query($sql);
-
// do an SQL UPDATE
-
}
-
else {
-
// do an SQL INSERT
-
}
New Way
SQL:
-
INSERT INTO Sales(TransId, STATUS, Amount)
-
VALUES(123, 'Pending', 20)
-
ON DUPLICATE KEY UPDATE STATUS = 'Paid'
Also keep in mind that a current bug in MySQL makes this method not replication safe. I believe this has been fixed.

My name is Noah Everett. I live in Tulsa, OK. I started 