IF语句的INSERT / UPDATE未完成

时间:2018-03-05 17:51:25

标签: php mysql

此代码目前在插入方面有效。但是,我已经用IF语句对它应用了一些逻辑,逻辑是这样的:

IF the record doesn't exist OR if it exists but today is after expire_date, just insert the record. If the record exists and today is before expire_date, update expire_date to 127 days from now

如果我截断我的数据库并运行此脚本,它会插入我期望的200行。这些记录中的大多数在今天之前都有expire_date,但出于测试目的,我将它们全部设置为明天到期。所以现在没有任何记录在今天之前到期。

如果我第二次运行此脚本,它会更新expire_date,因为今天是在expire_date之前,但它也会再次插入所有200条记录。

我的逻辑或我的IF语句在某处出错了,但是我一直没有解决它的问题。到目前为止,这是我最好的努力,它表现出上述症状。非常感谢任何指导

//Create prepared INSERT statement
$insertPlacement = "
INSERT ignore INTO placements_new (sku_id, group_id, dealer_id, start_date, expire_date, locations, order_num)
SELECT 
    id, 
    sku_group_id, 
    :DEALER, 
    DATE_ADD(DATE_FORMAT(CONVERT(:SHIPDATE, CHAR(20)), '%Y-%m-%d'),INTERVAL 7 DAY) as start_date,
    DATE_ADD(DATE_FORMAT(CONVERT(:SHIPDATE, CHAR(20)), '%Y-%m-%d'),INTERVAL 127 DAY) as expire_date, 
    :QUANTITY,
    :INVOICE  
FROM skus s  
WHERE  s.frame=:FRAME AND s.cover1=:COVER AND s.color1=:COLOR
";

//create update statement for necessary constraints
$updatePlacement = "
UPDATE placements_new 
SET expire_date = DATE_ADD(DATE_FORMAT(CONVERT(current_date(), CHAR(20)), '%Y-%m-%d'),INTERVAL 127 DAY)
";

//perpare query to check for existing records
$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);

    //print_r($count3);

}catch(PDOException $ex){
        echo "QUERY FAILED!!!: " . $ex->getMessage();
}



// IF records do not exist, or records exist and today is after expiration date
if(empty($count3) || strtotime(now()) > $count3['expire_date']){
    for($i=0; $i<$row2["QUANTITY"]; $i++) {  
        try{
            $insert = $MysqlConn->prepare($insertPlacement);
            $insertRslt = $insert->execute($values);
        }catch(PDOException $ex){
            echo "QUERY FAILED!!!: " . $ex->getMessage();
        }

    }
}else{
    for($i=0; $i<$row2['QUANTITY']; $i++){
        try{
            $update = $MysqlConn->prepare($updatePlacement);
            $updateRslt = $update->execute($values);
        }catch(PDOException $ex){
            echo "QUERY FAILED!!!: " . $ex->getMessage();
        }
    }
}

0 个答案:

没有答案