Jump to content

Running script on query with conditional update/insert loop


thenorman138

Recommended Posts

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.

This is the logic I have been working with:

 

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

 

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

so now I'm running this but it's not running and no errors, but it just returns back to the command line   

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

        
    for($i=0; $i<$row2['QUANTITY']; $i++){
        try{
            $updateRslt = $update->execute($updateParams);
        }catch(PDOException $ex){
            $failedUpdate = "UPDATE_FAILED";
            print_r($failedUpdate);
            $out[] = $failedUpdate;
        }
    }
    
        
    for($i=0; $i<$insert_count; $i++){
        try{
            $insertRslt = $insert->execute($insertParams);
        }catch(PDOException $ex){
            $failedInsertStore = "INSERT_STORE_FAILED!!!: " . $ex->getMessage();
            print_r($failedInsertStore);
            $out[] = $failedInsertStore;
        }
    }```

    

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.