简体   繁体   中英

PHP: Cant save the remeaning Data to the database

I am trying to insert the data to the database by fetching them first, doing some additions and then setting a condition in the loop, if the value exceeds over > 2200. Inside this if condition, I have a for each loop where it will take all the records fetched and insert into the 2nd table. I am getting it right so far, now the problem is the remaining value from the table fetched, does not insert into the new tables. Please find the screenshot attached (yellow cells). I want to also make them save and inserted in both the tables and assign a value to it.

在此处输入图片说明

Code

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

Output which I am getting it now: (When we add these total, the total is less than the required output)

在此处输入图片说明

Required Output: (when we do the loop < 2200, its not saving the value over 2200) 在此处输入图片说明

SQL" and thats the 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

Here is kind pseudo code:

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

NOTE You should stop using deprecated mysql_* functions. And you should use prepared statements with mysqli or PDO functions to avoid sql injections

How can I prevent SQL injection in PHP?

NOTE After chat some pseudocode to keep here: https://ideone.com/vRr5rA

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM