我正在尝试使用 PDO 修复 php 脚本中的 mysql 更新。
我有一个数组 $order_ids,其中包含正确的记录,然后我使用一个名为 $placeholders 的变量来为我的选择查询 $detailstatcheck 内爆这些值。
所有这些都有效,但我在底部添加的更新不起作用,当我运行脚本时,它会打印 0 条记录已更新。
我只想说“如果 order_id 在 $order_ids 或 $placeholders 中则更新记录”
$order_ids = [];
while ($row = $ordStat->fetch(PDO::FETCH_ASSOC)) {
$order_ids[] = $row['order_id'];
}
if (count($order_ids) > 0) {
$placeholders = implode(',', array_fill(0, count($order_ids), '?'));
//This query works
$detailStatCheck = "
SELECT
invnoc as INVOICE,
fstatc as STATUS,
cstnoc AS DEALER,
framec AS FRAME,
covr1c AS COVER,
colr1c AS COLOR ,
extd2d AS SHIPDATE,
orqtyc AS QUANTITY
FROM GPORPCFL
WHERE invnoc IN ($placeholders)
";
try {
$detailCheck = $DB2conn->prepare($detailStatCheck);
$detailRslt = $detailCheck->execute($order_ids);
$count2 = $detailCheck->fetch();
print_r($order_ids);
print_r($count2);
} catch(PDOException $ex) {
echo "QUERY FAILED!: " .$ex->getMessage();
}
//this update doesn't work
$updateShipped = "
UPDATE order_status
set date_updated = current_date()
where order_id in ($placeholders) //Not sure if syntax is correct here
";
try{
$updateStatus = $MysqlConn->prepare($updateShipped);
$statUpdateRslt = $updateStatus->execute();
$count = $updateStatus->rowcount();
}
catch(PDOException $ex)
{
echo "QUERY FAILED!: " .$ex->getMessage();
}
echo "Records Updated: " . $count . "\n";
更新:
order_status表的结构
order_id | order_status | is_placement | date_updated
-----------------------------------------------------------
12345 S 1 2018-03-09
order_id = int(11)
order_status = varchar
is_placement = 位
更新日期 = 日期
最佳答案
执行查询时缺少 $order_ids 参数:$statUpdateRslt = $updateStatus->execute($order_ids);
关于php - 在 MySQL PDO 查询中使用数组变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49186825/