MySql - ON DUPLICATE KEY INSERT

Viewed 7740

I understand that there exists INSERT IGNORE and INSERT ... ON DUPLICATE KEY UPDATE. However, when there is a duplicate key, I'd like to do a INSERT to a temporary table to keep a record of the unique key that has been violated, so that I can output it to the user.

Is there any way I can do a ON DUPLICATE INSERT? If it helps, I'm trying to do a bulk insert.

2 Answers

Maybe this could be helpful:

$time = time();
$countme = 1;
while ($countme > 0) {
    $stmt = $mysqli->prepare("INSERT INTO loads (dte,filecode,id,acct,nmbr) VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE dte = dte");
    $stmt->bind_param("sssss", $time, $filecode, $id, $acct, $number);
    $stmt->execute();
    $countme++;
    if ($stmt->affected_rows === 1) {
        $countme = 0; // all is good
    } else {
        $time = $time + 1;
    }
    if ($countme === 4) {
        exit;
    }
}
Related