php:无法将记忆数据保存到数据库中

nkhmeac6  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(389)

我正在尝试将数据插入到数据库中,方法是首先获取它们,进行一些添加,然后在循环中设置一个条件(如果值超过>2200)。在这个if条件中,我有一个for-each循环,它将获取所有记录并插入到第二个表中。到目前为止,我的想法是正确的,现在的问题是从表中提取的剩余值没有插入到新表中。请找到所附的截图(黄色单元格)。我还想让它们保存并插入到两个表中,并为其赋值。

代码

if  (isset($_POST["genRun"]))  {
  $total_weight = 0; 
  $i = 1;
  $arr = array();
  $excess = 0;

                mysql_select_db($database_callmtlc_SalmatDB, $callmtlc_SalmatDB);

                while($row_FetchRecordRS = mysql_fetch_assoc($FetchRecordRS))   {

                 $id = $row_FetchRecordRS['ID'];
                $carr_ID = $row_FetchRecordRS['CarrierID'];
                $address = $row_FetchRecordRS['DeliveryAddress'];
                $potzone = $row_FetchRecordRS['Postzone'];
                $instruction = $row_FetchRecordRS['DeliveryInstruction'];
                $quantity = $row_FetchRecordRS['Quantity'];
                $jobID = $row_FetchRecordRS['JobID'];
                $jobName = $row_FetchRecordRS['JobName'];
                $bundlesize = $row_FetchRecordRS['Bundlesize'];
                $bundle = $row_FetchRecordRS['Bundles'];
                $items = $row_FetchRecordRS['Items'];
                $weight = $row_FetchRecordRS['WeightKgs'];
                $suburb = $row_FetchRecordRS['Suburb'];
                $num = $row_FetchRecordRS['TotalWeightKgs'];

               //$num = $row_FetchRecordRS['TotalWeightKgs'];

                $arr[] = array('CarrierID' => $carr_ID, 'DeliveryAddress' => $address, 'Postzone' => $potzone, 'DeliveryInstruction' => $instruction, 'Quantity' => $quantity, 'JobID' => $jobID, 'JobName' => $jobName, 'Bundlesize' => $bundlesize, 'Bundles' => $bundle, 'Items' => $items, 'WeightKgs' => $weight, 'Suburb' => $suburb, 'TotalWeightKgs' => $num);

                      if ($num + $total > 2200) {
                            $sqltransitlist = "INSERT INTO TransitList(genID, total) Values ('$i','$total')";
                            $ResultUpd3 = mysql_query($sqltransitlist, $callmtlc_SalmatDB);

                            foreach ($arr as $data) {
                                $sqlquerytest = "INSERT INTO GenerateRun(CarrierID, DeliveryAddress, Postzone,DeliveryInstruction, Quantity, JobID, JobName, 
                     Bundlesize, Bundles, Items, WeightKgs,  Suburb, TotalWeightKgs,LodingZoneID) VALUES('" 
                     . $data['CarrierID'] ."','"  . $data['DeliveryAddress'] ."','" . $data['Postzone'] . "','"  .  $data['DeliveryInstruction']. "','" .$data['Quantity']  . "','"  . $data['JobID'] . "','"  . $data['JobName'] . "','" . $data['Bundlesize']. "','"  .$data['Bundles'] . "','"    . $data['Items'] . "','"  .$data['WeightKgs']. "','" . $data['Suburb']."','"  .$num."','" .$i ."')";
                                $ResultUpd1 = mysql_query($sqlquerytest, $callmtlc_SalmatDB); 
                             }

        $arr = array();  
        $i++;
        $total = 0;   
  } else {
        $total += $num;
  }
}
// after the loop check if there are some data was not inserted and insert it after the loop is over
if ($total > 0) {
   $sqltransitlist = "INSERT INTO TransitList(genID, total) Values ('$i','$total')";
   $ResultUpd3 = mysql_query($sqltransitlist, $callmtlc_SalmatDB);
   foreach ($arr as $data) {
       $sqlquerytest = "INSERT INTO GenerateRun(CarrierID, DeliveryAddress, Postzone,DeliveryInstruction, Quantity, JobID, JobName, 
                     Bundlesize, Bundles, Items, WeightKgs,  Suburb, TotalWeightKgs,LodingZoneID) VALUES('" 
                     . $data['CarrierID'] ."','"  . $data['DeliveryAddress'] ."','" . $data['Postzone'] . "','"  .  $data['DeliveryInstruction']. "','" .$data['Quantity']  . "','"  . $data['JobID'] . "','"  . $data['JobName'] . "','" . $data['Bundlesize']. "','"  .$data['Bundles'] . "','"    . $data['Items'] . "','"  .$data['WeightKgs']. "','" . $data['Suburb']."','"  .$num."','" .$i ."')";

                     echo "$i , $total <br>";
       $ResultUpd1 = mysql_query($sqlquerytest, $callmtlc_SalmatDB); 
   }
}

我现在得到的输出:(当我们加上这些总数时,总数小于所需的输出)

要求输出:(当我们做循环<2200时,它不保存超过2200的值)

sql“这就是sql:

SELECT UpdatedCsvFiles.ID, UpdatedCsvFiles.CarrierID, UpdatedCsvFiles.DeliveryAddress, UpdatedCsvFiles.Postzone, UpdatedCsvFiles.DeliveryInstruction, UpdatedCsvFiles.Quantity, UpdatedCsvFiles.JobID, UpdatedCsvFiles.JobName, UpdatedCsvFiles.Bundlesize, UpdatedCsvFiles.Bundles, UpdatedCsvFiles.Items, UpdatedCsvFiles.WeightKgs, SuburbPostZone.Suburb, UpdatedCsvFiles.TotalWeightKgs FROM UpdatedCsvFiles LEFT JOIN SuburbPostZone on SuburbPostZone.areaID = UpdatedCsvFiles.CarrierID Where UpdatedCsvFiles.DeliveryAddress != 'PLEASE LEAVE IN WAREHOUSE' GROUP by  UpdatedCsvFiles.CarrierID, UpdatedCsvFiles.DeliveryAddress ORDER by UpdatedCsvFiles.CarrierID , SuburbPostZone.Suburb, UpdatedCsvFiles.ID  ASC
xxls0lw8

xxls0lw81#

以下是伪代码:

// select db just once before the loop, you don't need to select db every time in the loop
mysql_select_db($database_callmtlc_SalmatDB, $callmtlc_SalmatDB);
// use while loop
while($row_FetchRecordRS = mysql_fetch_assoc($FetchRecordRS)) {
  $id = $row_FetchRecordRS['ID'];
  ...
  if ($num + $total > 2200) {
        $sqltransitlist = "INSERT INTO TransitList(genID, total) ...";
        $ResultUpd3 = mysql_query($sqltransitlist, $callmtlc_SalmatDB);

        foreach ($arr as $data) {
            $sqlquerytest = "INSERT INTO GenerateRun(CarrierID ...";
            $ResultUpd1 = mysql_query($sqlquerytest, $callmtlc_SalmatDB); 
        }

        $arr = array();  
        $i++;
        $total = 0;   
  } 

  $total += $num;
  $arr[] = array('CarrierID' => $carr_ID, 'DeliveryAddress'...);
}
// after the loop check if there are some data was not inserted and insert it after the loop is over
if ($total > 0) {
   $sqltransitlist = "INSERT INTO TransitList(genID, total) ...";
   $ResultUpd3 = mysql_query($sqltransitlist, $callmtlc_SalmatDB);
   foreach ($arr as $data) {
       $sqlquerytest = "INSERT INTO GenerateRun(CarrierID ...";
       $ResultUpd1 = mysql_query($sqlquerytest, $callmtlc_SalmatDB); 
   }
}

注意:您应该停止使用不推荐使用的mysql函数。您应该将准备好的语句与mysqli或pdo函数一起使用,以避免sql注入
如何防止php中的sql注入?
请注意,在聊天后,这里需要保留一些伪代码:https://ideone.com/vrr5ra

相关问题