gpt4 book ai didi

php - 使用准备好的 SQL 语句捕获重复条目

转载 作者:行者123 更新时间:2023-11-29 03:30:22 25 4
gpt4 key购买 nike

我想用准备好的 MySQL 语句处理“重复输入”错误。

我有那些台词

$result = $db->prepare("INSERT INTO comptability (id_comptability, order_id, Reduction, `%TVA`, Facture) VALUES (?, ?, ?, ?, ?)");
$result->execute(array($no_facture, $id_order, $reduc_tot, $tot, $date));

并希望在没有任何错误的情况下执行一些代码。并在出现错误时显示一个弹出窗口并使 prog 死亡。

我已经试过了

if($result === false)//Catch error
{}
else //It worked
{}

但是没有效果

最佳答案

您可以使用 INSERT IGNORE,然后使用 PDOStatement::rowCount 检查受影响的行数

概念应遵循:

$result = $db->prepare("INSERT IGNORE INTO comptability (id_comptability, order_id, Reduction, `%TVA`, Facture) VALUES (?, ?, ?, ?, ?)");
if ($result->execute(array($no_facture, $id_order, $reduc_tot, $tot, $date))) {
if ($result->rowCount() == 0) {
// You had a duplicate record.
} else {
// all good.
}
}

关于php - 使用准备好的 SQL 语句捕获重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31470971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com