Skip to content

Instantly share code, notes, and snippets.

@chonthu
Created February 23, 2012 16:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
on_duplicate_update 2
INSERT INTO
`product`
SET
`id`=?
,`name`=?
,`created_at`=NOW()
ON DUPLICATE KEY UPDATE
,`name`=?
';
$stmt = mysqli_prepare($dbh, $sql);
foreach ($products as $p) {
mysqli_stmt_bind_param($stmt, 'ss'.'ss',
$p['id']
,$p['name']
,$p['name']
);
$success = mysqli_stmt_execute($stmt);
$err = mysqli_error($dbh);
if (!$success && strpos($err, 'Duplicate entry')) {
echo "Error inserting product: ";
echo $err;
echo PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment