我有一个正在运行的php脚本,该脚本似乎一直在工作,但是现在我想知道我的某些逻辑是否有可能关闭。
我从数据库表中选择一个日期范围内的记录,并将其放入名为$validCount
的数组中
如果该数组不为空,则意味着我有有效的记录可以使用我的值进行更新,如果为空,则只需插入。插入的技巧是,如果STORES
小于Quantity
,则插入的数量最多仅为STORES
,否则插入的数量最多为Quantity
。>
因此,如果要插入的记录具有
Stores: 14 Quantity:12
那么它只会插入12条记录,如果有的话
Stores:1 Quantity:20
它只会插入1条记录。
简而言之,对于每个客户,我应该只拥有与商店相同数量的有效记录(在有效日期范围内)。如果他们有20家商店,我可以拥有1或2条记录,但绝对不能有30条记录。
似乎更新工作正常,但我不确定是否更新了正确的记录,尽管在某些情况下似乎只是插入了太多记录而不考虑过去的更新记录。
这里的逻辑看起来有什么不对劲吗?
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个营业地点
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条记录的到期日期。
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条(商店数量)记录。该客户/产品的行数达到商店的等效值后,它现在需要遍历并更新等于数量的
更新
当前正在运行:
$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;
}
}
答案 0 :(得分:2)
我认为逻辑应该是这样的:
$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
}
然后使用$insert_count
作为for
循环中用于插入新行的重复计数。 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;
}
}