简体   繁体   中英

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.

I select records from a db table within a date range which I put into an array called $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 .

So if a record being inserted with had

Stores: 14 Quantity:12

Then it would only insert 12 records but if it had

Stores:1  Quantity:20

It would only insert 1 record.

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.

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

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. 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. Let's say they bought 15 this time. Then I would need to insert 6 records, and then update the expiration date of the other 9 records.

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. 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. The UPDATE query should include LIMIT :limit , and then you can bind $update_count to the :limit placeholder.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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