mysql数据库值进行比较数组值与PDO
问题描述:
我不能比较这阵mysql数据库值进行比较数组值与PDO
$products = [];
foreach (array_keys($match[2]) as $idx)
{
$tagliaprodotto = rtrim(substr($match[2][$idx],1));
$tagliaquantita = ltrim($match[3][$idx],'0');
$products[] = [
'product' => $tagliaprodotto,
'quantity' => $tagliaquantita,
'terminal' => $match[4][$idx]
];
}
与数据库内的值的值!我应该这样做吗?
我想比较,我有在数据库内的元素与数组的值,使一些if为:
- 如果产品不存在于数据库中:
INSERT INTO table (product, quantity, terminal, date) VALUES (:product, :quantity, :terminal, NOW())
- 如果数据库记录完美匹配:产品,数量和终端:Do nothing
- 如果数据库记录包含:相同产品但数量不同和/ o [R终端:
UPDATE table SET quantity=:quantity, terminal=:terminal
答
我假设你指数product
,否则这得到棘手。使用的ON DUPLICATE KEY
和IF()
组合:
INSERT INTO table (product, quantity, terminal, date)
VALUES (:product, :quantity, :terminal, NOW())
ON DUPLICATE KEY UPDATE
quantity = IF(quantity != :quantity AND terminal = :terminal, :quantity, quantity),
terminal = IF(quantity = :quantity AND terminal != :terminal, :terminal, terminal)
如果有不product
指数(或任何指定的列),该UPDATE
将永远不会触发。如果查询中存在多个列的索引,则会出现您不打算更新的行得到更新的问题,尤其是在索引不唯一的情况下。
https://*.com/questions/23305300/check-if-username-exists-pdo –
我没有单个变量$ user ='userName'; – Squalo
这是演示例子,所以你可以更新你的代码,就像它 –