我在PDO语句中有一个工作插入,但是我试图在它周围添加一个if语句来进行日期检查以及现有记录。
如果我运行它只是为了检查现有记录,如下:
$dealerSkuCheck = "
SELECT
sku_id,
dealer_id,
expire_date
FROM placements_new p
INNER JOIN skus s
ON p.sku_id = s.id
WHERE p.dealer_id = :DEALER
AND s.frame = :FRAME
AND s.cover1 = :COVER
AND s.color1 = :COLOR
AND p.order_num = :INVOICE
";
while ($row2 = $detailCheck->fetch(PDO::FETCH_ASSOC)) {
$values = [
":DEALER" => $row2["DEALER"],
":SHIPDATE" => $row2["SHIPDATE"],
":QUANTITY" => $row2["QUANTITY"],
":INVOICE" => $row2["INVOICE"],
":FRAME" => $row2["FRAME"],
":COVER" => $row2["COVER"],
":COLOR" => $row2["COLOR"],
];
$values2 = [
":DEALER" => $row2["DEALER"],
":FRAME" => $row2["FRAME"],
":COVER" => $row2["COVER"],
":COLOR" => $row2["COLOR"],
":INVOICE" => $row2["INVOICE"],
];
try{
$checkExisting = $MysqlConn->prepare($dealerSkuCheck);
$existingRslt = $checkExisting->execute($values2);
$count3 = $checkExisting->fetch(PDO::FETCH_ASSOC);
while($row3 = $checkExisting->fetch(PDO::FETCH_ASSOC)){
$expiration[] = $row3['expire_date'];
}
}catch(PDOException $ex){
echo "QUERY FAILED!!!: " . $ex->getMessage();
}
//print_r($count3);
// IF records do not exist, or records exist and today is after expiration date
if(empty($count3) ){
for($i=0; $i<$row2["QUANTITY"]; $i++) {
try{
$insert = $MysqlConn->prepare($insertPlacement);
$insertRslt = $insert->execute($values);
}catch(PDOException $ex){
echo "QUERY FAILED!!!: " . $ex->getMessage();
}
}
}
}
完美无缺。它匹配存在的记录并绕过它们,并插入任何不存在的记录,就像我期望的那样。
但是,当我将日期检查添加到if语句时,如下所示:
if(empty($count3) || strtotime($expiration) < strtotime('now')
它没有用,我得到了strtotime() expects param 1 to be string, array given
这显然是因为我将expire_date
存储在一个数组中,但我似乎无法解决如何修改它以正确读取日期。
同样,其他一切都有效但我只想让我的if语句说if the record doesn't exist OR if it exists but today is after the expire_date of that record, then perform the following insert
。
我在阅读那个日期时显然做错了什么,但我完全迷失了。
答案 0 :(得分:1)
$expiration
是一个数组。如果您在内部while循环中添加条件,则需要直接从查询结果中引用过期日期:
if (empty($count3) || strtotime($row3['expire_date']) < strtotime('now'))