Pdo多次插入记录,等于数据库值

时间:2018-03-05 05:03:46

标签: php mysql pdo

我有一个脚本,它可以选择数据,创建数组和插入数据(所有使用PDO)。我遇到的唯一问题是我需要修改我的插入和更新方式。

很简单,每个不同的记录都需要以重复的方式插入(这听起来很疯狂,我把它带到了我的团队领导者,但我们的共识是我们希望每条记录都有自己的日期字段,因为只有一些将会被更新)。这听起来应该不是那么困难,但我从来没有做过我没有使用UPDATE ON DUPLICATE KEY或INSERT IGNORE的东西。这实际上是要复制某些记录。

基本上这完全取决于我第一次选择的orqtyc AS QUANTITY字段。

我需要构建几个if语句,但主要问题是我首先要说的是```WHILE insert count is< :QUANTITY,继续插入,直到插入数等于:QUANTITY

这有意义吗?如果没有,我可以澄清一下。

基本上我需要帮助做一些逻辑来修改我执行这个已经工作的插入的方式。我可以创建我需要的if / else语句,但这是我需要的第一件事,因为要完成的任何INSERT或UPDATE必须等于该订单的数量字段。

所以基本上对于插入的每个记录,应该插入'n'个记录。如果记录号1的数量为1,则只插入1.如果记录2的数量为10,则插入该记录10次。

$order_ids = [];
while ($row = $ordStat->fetch(PDO::FETCH_ASSOC)) {
$order_ids[] = $row['order_id'];
}

if (count($order_ids) > 0) {
$placeholders = implode(',', array_fill(0, count($order_ids), '?'));
$detailStatCheck = "
    SELECT 
         invnoc as INVOICE,
         fstatc as STATUS,
         cstnoc AS DEALER,
         framec AS FRAME,
         covr1c AS COVER,
         colr1c AS COLOR ,
         extd2d AS SHIPDATE,
         orqtyc AS QUANTITY
    FROM GPORPCFL
    WHERE invnoc IN ($placeholders)
";

try {
    $detailCheck = $DB2conn->prepare($detailStatCheck);
    $detailRslt = $detailCheck->execute($order_ids);
    $count2 = $detailCheck->fetch();
    print_r($order_ids);
    print_r($count2);
} catch(PDOException $ex) {
    echo "QUERY FAILED!: " .$ex->getMessage();
}


while ($row2 = $detailCheck->fetch(PDO::FETCH_ASSOC)){


 //IF exists and today is before expire date
 //update records = quantity, or insert

 //ELSEIF exists and today is  past expire_date, just insert number for each quantity

 //ELSE doesn't exist at all, perform below

$values = [
 ":DEALER" => $row2["DEALER"],
 ":SHIPDATE" => $row2["SHIPDATE"],
 ":QUANTITY" => $row2["QUANTITY"],
 ":INVOICE" => $row2["INVOICE"],
 ":FRAME" => $row2["FRAME"],
 ":COVER" => $row2["COVER"],
 ":COLOR" => $row2["COLOR"],
];


 $insertPlacement = "
 INSERT INTO placements_new (sku_id, group_id, dealer_id, start_date, expire_date, locations, order_num)
 SELECT 
  id, 
  sku_group_id, 
  :DEALER, 
  DATE_ADD(DATE_FORMAT(CONVERT(:SHIPDATE, CHAR(20)), '%Y-%m-%d'),INTERVAL 7 DAY) as start_date,
  DATE_ADD(DATE_FORMAT(CONVERT(:SHIPDATE, CHAR(20)), '%Y-%m-%d'),INTERVAL 127 DAY) as expire_date, 
  :QUANTITY,
  :INVOICE  
FROM skus s  
  WHERE  s.frame=:FRAME AND s.cover1=:COVER AND s.color1=:COLOR
 ";
 try{
 $insert = $MysqlConn->prepare($insertPlacement);
 $insertRslt = $insert->execute($values);
 }catch(PDOException $ex){
 echo "QUERY FAILED!!!: " . $ex->getMessage();
 }
 }
}

1 个答案:

答案 0 :(得分:0)

所以,我正在过度思考这个问题并为QUANTITY值添加一个迭代器是最简单的方法

 while ($row2 = $detailCheck->fetch(PDO::FETCH_ASSOC)) {

    //IF exists and today is before expire date
    //update records = quantity, or insert
    //ELSEIF exists and today is  past expire_date, just insert number for each quantity
    //ELSE doesn't exist at all, perform below

    $values = [
        ":DEALER" => $row2["DEALER"],
        ":SHIPDATE" => $row2["SHIPDATE"],
        ":QUANTITY" => $row2["QUANTITY"],
        ":INVOICE" => $row2["INVOICE"],
        ":FRAME" => $row2["FRAME"],
        ":COVER" => $row2["COVER"],
        ":COLOR" => $row2["COLOR"],
    ];

    for($i=0; $i<$row2["QUANTITY"]; $i++) {

        try{
            $insert = $MysqlConn->prepare($insertPlacement);
            $insertRslt = $insert->execute($values);
        }catch(PDOException $ex){
            echo "QUERY FAILED!!!: " . $ex->getMessage();
        }

    }
}