INSERT ... ON DUPLICATE UPDATE仅插入

时间:2017-10-27 03:54:05

标签: mysql on-duplicate-key

我目前正在使用MySQL和PHP 7的PDO,我试图将客户插入或更新到数据表。我目前使用的代码是:

$stmt = $db->prepare("
INSERT INTO `customer` (
  `customer_id`,
  `household_id`,
  `first_name`,
  `last_name`,
  `suffix_id`,
  `birthday`,
  `customer_since`,
  `referral_id`,
  `phone`,
  `email`,
  `address`,
  `city`,
  `state_id`,
  `zip_id`)
VALUES(
  :customer_id,
  :household_id,
  :first_name,
  :last_name,
  :suffix_id,
  :birthday,
  :customer_since,
  :referral_id,
  :phone,
  :email,
  :address,
  :city,
  :state,
  :zip_id
) ON DUPLICATE KEY UPDATE
  `customer_id` = :customer_id2,
  `household_id` = :household_id2,
  `first_name` = :first_name2,
  `last_name` = :last_name2,
  `suffix_id` = :suffix_id2,
  `birthday` = :birthday2,
  `customer_since` = :customer_since2,
  `referral_id` = :referral_id2,
  `phone` = :phone2,
  `email` = :email2,
  `address` = :address2,
  `city` = :city2,
  `state_id` = :state2,
  `zip_id` = :zip_id2;");

// Parameterize the query 
$stmt->bindValue(':customer_id', (isset($_POST['customer_id']) || empty($_POST['customer_id'])) ? null : $_POST['customer_id']);
$stmt->bindValue(':household_id', $household_id, PDO::PARAM_INT);
$stmt->bindValue(':first_name', $_POST['first_name'], PDO::PARAM_STR);
$stmt->bindValue(':last_name', $_POST['last_name'], PDO::PARAM_STR);
$stmt->bindValue(':suffix_id', $suffix_id, PDO::PARAM_STR);
$stmt->bindValue(':birthday', $_POST['birthday'], PDO::PARAM_STR);
$stmt->bindValue(':customer_since', (isset($_POST['customer_since']) || empty($_POST['customer_since'])) ? null : $_POST['customer_since'], PDO::PARAM_STR);
$stmt->bindValue(':referral_id', $referral_id, PDO::PARAM_STR);
$stmt->bindValue(':phone', (isset($_POST['phone']) || empty($_POST['phone'])) ? null : $_POST['phone'], PDO::PARAM_STR);
$stmt->bindValue(':email', (isset($_POST['email']) || empty($_POST['email'])) ? null : $_POST['email'], PDO::PARAM_STR);
$stmt->bindValue(':address', $_POST['street'], PDO::PARAM_STR);
$stmt->bindValue(':city', $_POST['city'], PDO::PARAM_STR);
$stmt->bindValue(':state', $_POST['state'], PDO::PARAM_INT);
$stmt->bindValue(':zip_id', $zip_id, PDO::PARAM_INT);

$stmt->bindValue(':customer_id2', (isset($_POST['customer_id']) || empty($_POST['customer_id'])) ? null : $_POST['customer_id'], PDO::PARAM_INT);
$stmt->bindValue(':household_id2', $household_id, PDO::PARAM_INT);
$stmt->bindValue(':first_name2', $_POST['first_name'], PDO::PARAM_STR);
$stmt->bindValue(':last_name2', $_POST['last_name'], PDO::PARAM_STR);
$stmt->bindValue(':suffix_id2', $suffix_id, PDO::PARAM_STR);
$stmt->bindValue(':birthday2', $_POST['birthday'], PDO::PARAM_STR);
$stmt->bindValue(':customer_since2', (isset($_POST['customer_since']) || empty($_POST['customer_since'])) ? null : $_POST['customer_since'], PDO::PARAM_STR);
$stmt->bindValue(':referral_id2', $referral_id, PDO::PARAM_STR);
$stmt->bindValue(':phone2', (isset($_POST['phone']) || empty($_POST['phone'])) ? null : $_POST['phone'], PDO::PARAM_STR);
$stmt->bindValue(':email2', (isset($_POST['email']) || empty($_POST['email'])) ? null : $_POST['email'], PDO::PARAM_STR);
$stmt->bindValue(':address2', $_POST['street'], PDO::PARAM_STR);
$stmt->bindValue(':city2', $_POST['city'], PDO::PARAM_STR);
$stmt->bindValue(':state2', $_POST['state'], PDO::PARAM_INT);
$stmt->bindValue(':zip_id2', $zip_id, PDO::PARAM_INT);

customer_id字段是customer数据表的唯一自动生成字段。

每当我创建一个新客户时,它都可以正常工作。但是,每当我尝试更新现有客户时,它都会在customer表中创建一个新行,而不是使用新的自动递增policy_id值更新现有行。我甚至通过调用echo $_POST['customer_id']确认有一个customer_id值,以确保每当我希望它更新时都有值。

我做错了什么?

更新

我的原帖中有一个拼写错误,我说policy_id,但我想说customer_id。 此外,customer_id是主键,是唯一的主要自动增量字段。

更新#2

即使有人自己去删除我的PHP和PDO标签,这里是$ _POST的输出:

[household_id]   => 1
[customer_id]    => 1
[first_name]     => Jane
[last_name]      => Doe
[suffix]         => 
[phone]          => (555) 555-1234
[email]          => user@email.com
[street]         => 1234 Main St
[city]           => Anytown
[state]          => 1
[zip]            => 12345
[birthday]       => 2017-10-27
[customer_since] => 2017-10-27

我也在以下值上成功使用Insert ... On Duplicate Key:

  • Household_ID
  • 后缀
  • 推荐
  • 邮编

这些值会在各自的表中插入一个新值,然后生成相应的外键或更新现有值以保持不变。

0 个答案:

没有答案
相关问题