首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP循环遍历查询以执行条件插入或更新

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

Stack Overflow用户
提问于 2019-01-08 04:38:06
回答 1查看 63关注 0票数 0

我有一个我一直在运行的php脚本,它似乎一直在工作,但现在我想知道我的一些逻辑是否可能是错误的。

我从db表中选择日期范围内的记录,并将其放入名为$validCount的数组中。

如果数组不是空的,这意味着我有有效的记录要用我的值更新,如果它是空的,我就插入。插入的诀窍是,如果STORES小于Quantity,则它只插入与STORES一样多的内容,否则它将插入与Quantity一样多的内容。

因此,如果使用had插入记录

代码语言:javascript
复制
Stores: 14 Quantity:12

那么它只会插入12条记录,但如果有

代码语言:javascript
复制
Stores:1  Quantity:20

它只会插入一条记录。

简而言之,对于每个客户,我应该只拥有与他们拥有的商店一样多的有效记录(在有效日期范围内)。如果他们有20个商店,我可以有1到2个记录,但不应该有30个。

更新看起来很好,但我不确定它是否更新了正确的记录,尽管在某些情况下,它似乎只是插入了太多记录,而没有考虑过去更新的记录。

这里的逻辑有没有完全不正确的地方?

代码语言:javascript
复制
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;
            }
        }
    }
}

更新:

假设客户123购买了4个产品A,他们有10个地点

代码语言:javascript
复制
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

因为他们购买的数量少于他们的商店数量,所以我插入了4条记录。现在,如果我的$validCheck查询选择了所有4条记录(因为它们在有效的日期范围内),并且我的循环发现数组不是空的,那么它就知道需要更新这些记录或插入这些记录。假设他们这次买了15个。然后,我需要插入6条记录,然后更新其他9条记录的到期日期。

代码语言:javascript
复制
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

在有效日期范围内,该客户和产品最多只能有10条(门店数量)记录。一旦该客户/产品的行数达到商店的等价物,它现在就需要遍历并更新等于数量的值

更新

当前正在运行此命令:

代码语言:javascript
复制
    $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;
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-08 06:02:07

我认为逻辑应该是这样的:

代码语言:javascript
复制
$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循环中使用$insert_count作为重复计数。UPDATE查询应该包含LIMIT :limit,然后就可以将$update_count绑定到:limit占位符。

代码语言:javascript
复制
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;
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54081530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档