简体   繁体   English

PHP遍历查询以执行条件插入或更新

[英]PHP looping over query to perform conditional inserts or updates

I have a php script that I've been running that seems to have been working but now I'm wondering if some of my logic is potentially off. 我有一个正在运行的php脚本,该脚本似乎一直在工作,但现在我想知道我的某些逻辑是否可能关闭。

I select records from a db table within a date range which I put into an array called $validCount 我从数据库表中选择一个日期范围内的记录,并将其放入名为$validCount的数组中

If that array is not empty, that means I have valid records to update with my values, and if it's empty I just insert. 如果该数组不为空,则意味着我有有效的记录可以使用我的值进行更新,如果为空,则只需插入。 The trick with the insert is that if the STORES is less than the Quantity then it only inserts as many as the STORES otherwise it inserts as many as Quantity . 插入的技巧是,如果STORES小于“ Quantity则仅插入与STORES一样多的Quantity否则将插入与“ Quantity一样多的Quantity

So if a record being inserted with had 因此,如果插入的记录具有

Stores: 14 Quantity:12

Then it would only insert 12 records but if it had 那么它只会插入12条记录,但是如果有

Stores:1  Quantity:20

It would only insert 1 record. 它只会插入1条记录。

In short, for each customer I should only ever have as many valid records (within a valid date range) as they have stores. 简而言之,对于每个客户,我应该只拥有与商店一样多的有效记录(在有效日期范围内)。 If they have 20 stores, I can have 1 or 2 records but should never have 30. 如果他们有20家商店,我可以拥有1或2条记录,但绝对不能有30条。

It seems like updating works fine but I'm not sure if it's updating the proper records, though it seems like in some instances it's just inserting too many and not accounting for past updated records. 似乎更新工作正常,但我不确定是否要更新适当的记录,尽管在某些情况下似乎只是插入了太多记录而不考虑过去的更新记录。

Does anything seem outright incorrect with the logic here? 这里的逻辑看起来有什么不对劲吗?

if(!empty($validCount)){
        for($i=0; $i<$row2['QUANTITY']; $i++){
            try{
                $updateRslt = $update->execute($updateParams);
            }catch(PDOException $ex){
                $out[] = $failedUpdate;
            }
        }
}else{  
    if($row2["QUANTITY"] >= $row2["STORES"]){
        for($i=0; $i<$row2["STORES"]; $i++){     
            try{
                $insertRslt = $insert->execute($insertParams);
            }catch(PDOException $ex){
                $out[] = $failedInsertStore;
            }
        }
    }elseif($row2["QUANTITY"] < $row2["STORES"]){
        for($i=0; $i<$row2["QUANTITY"]; $i++){   
            try{
                $insertRslt = $insert->execute($insertParams);
            }catch(PDOException $ex){
                $out[] = $failedInsertQuantity;
            }
        }
    }
}

Update: 更新:

Let's say customer 123 bought 4 of product A and they have 10 locations 假设客户123购买了产品A的4个,并且有10个地点

customerNumber  |  product  |  category  |  startDate  |  expireDate  | stores
----------------------------------------------------------------------------------
123                 1           A           2018-08-01    2019-03-01      10
123                 1           A           2018-08-01    2019-03-01      10
123                 1           A           2018-08-01    2019-03-01      10
123                 1           A           2018-08-01    2019-03-01      10

Because they purchased less than their store count, I insert 4 records. 由于他们购买的商品少于商店数量,因此我插入了4条记录。 Now if my $validCheck query selects all 4 of those records (since they fall in a valid date range) and my loop sees that the array isn't empty, it knows it needs to update those or insert. 现在,如果我的$validCheck查询选择了所有4条记录(因为它们属于有效日期范围),并且我的循环看到该数组不为空,它知道需要更新这些记录或插入。 Let's say they bought 15 this time. 假设他们这次买了15张。 Then I would need to insert 6 records, and then update the expiration date of the other 9 records. 然后,我需要插入6条记录,然后更新其他9条记录的到期日期。

customerNumber  |  product  |  category  |  startDate  |  expireDate  | stores
----------------------------------------------------------------------------------
123                 1           A           2018-08-01    2019-03-11      10
123                 1           A           2018-08-01    2019-03-11      10
123                 1           A           2018-08-01    2019-03-11      10
123                 1           A           2018-08-01    2019-03-11      10
123                 1           A           2018-08-01    2019-03-11      10
123                 1           A           2018-08-01    2019-03-11      10
123                 1           A           2018-08-01    2019-03-11      10
123                 1           A           2018-08-01    2019-03-11      10
123                 1           A           2018-08-01    2019-03-11      10
123                 1           A           2018-08-01    2019-03-11      10

There can only ever be a maximum of 10 (store count) records for that customer and product within the valid date range. 在有效日期范围内,该客户和产品最多只能有10条(商店数量)记录。 As soon as the row count for that customer/product reaches the equivalent of stores, it needs to now go through and update equal to the quantity 该客户/产品的行数达到商店的等效值后,现在需要遍历并更新等于数量

Update 更新资料

Currently running this: 当前正在运行:

    $total = $row2['QUANTITY'] + $validCheck;
    if ($total < $row2['STORES']) {
        $insert_count = $row2['QUANTITY'];
        $update_count = 0;
    } else {
        $insert_count = $row2['STORES'] - $validCheck; // insert enough to fill all stores
        $update_count = ($total - $insert_count); // update remainder
    }

    foreach ($i = 0; $i < $insert_count; $i++) {
        try {
            $insertRslt = $insert->execute($insertParams);
        } catch(PDOException $ex){
            $out[] = $failedInsertStore;
        }
    }
    if ($update_count > 0) {
        try {
            $updateParams[':UPDATELIMIT'] = $update_count;
            $updateRslt = $update->execute($updateParams);
        }  catch(PDOException $ex){
            $out[] = $failedInsertStore;
        }
    }

I think the logic should be like this: 我认为逻辑应该是这样的:

$total = $row2['QUANTITY'] + $validCheck;
if ($total < $row2['STORES']) {
    $insert_count = $row2['QUANTITY'];
    $update_count = 0;
} else {
    $insert_count = $row2['STORES'] - $validCheck; // insert enough to fill all stores
    $update_count = ($total - $insert_count); // update remainder
}

Then use $insert_count as the repeat count in the for loop for inserting new rows. 然后在插入循环的for循环中使用$insert_count作为重复计数。 The UPDATE query should include LIMIT :limit , and then you can bind $update_count to the :limit placeholder. UPDATE查询应包含LIMIT :limit ,然后可以将$update_count绑定到:limit占位符。

for ($i = 0; $i < $insert_count; $i++) {
    try {
        $insertRslt = $insert->execute($insertParams);
    } catch(PDOException $ex){
        $out[] = $failedInsertStore;
    }
}
if ($update_count > 0) {
    try {
        $updateParams[':limit'] = $update_count;
        $updateRslt = $update->execute($updateParams);
    }  catch(PDOException $ex){
        $out[] = $failedInsertStore;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM