PHP:无法将重新定义的数据保存到数据库

时间:2018-07-26 00:32:33

标签: php mysql loops

我试图通过先获取数据,进行一些添加然后在循环中设置条件(如果值超过> 2200)来将数据插入数据库。在此if条件中,每个循环都有一个它将获取所有记录并插入第二张表。到目前为止,我说对了,现在的问题是从表中获取的剩余值没有插入新表中。请找到所附的屏幕截图(黄色单元格)。我还希望将它们保存并插入两个表中,并为其分配一个值。

enter image description here

代码

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

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

enter image description here

必需的输出:(当我们执行循环<2200时,它没有保存超过2200的值) enter image description here

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

1 个答案:

答案 0 :(得分:1)

这是一种伪代码:

// 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注入

How can I prevent SQL injection in PHP?

注意聊天后,一些伪代码保留在此处:https://ideone.com/vRr5rA