douyi1855
2018-07-25 02:29
浏览 89
已采纳

如何获取记录总和为一个值,然后添加到两个新表?

I am trying to sum (add the values) from the database. My application checks the values from each row, adds up the value from each row up to 2000. And once it reaches up to 2000, it saves in the database (insert query) and continues the same till last record fetched. The total value summed (or totaled) up by each rows should not exceed over 2000.

There are two insert queries, One for inserting the total( from each row between 1800 and 2000) with the ID (like Primary key) generated and the second table add each row inserted with ID (the ID generated becomes now foreign key)

Please refer to the screenshot.

enter image description here Please find the code below:

$i = 1;
do {                
    $id = $row_FetchRecordRS['ID'];
    $dateissued = $row_FetchRecordRS['DateIssued'];
    $rundateCarrierRun = $row_FetchRecordRS['RundateCarrierRunID'];
    $timegenerated = $row_FetchRecordRS['TimeGenerated'];
    $carrierID = $row_FetchRecordRS['CarrierRunID'] ;
    $areaID = $row_FetchRecordRS['CarrierAreaID'];
    $address = $row_FetchRecordRS['DeliveryAddress'];
    $potzone = $row_FetchRecordRS['Postzone'];
    $carr_ID = $row_FetchRecordRS['CarrierID'];
    $instruction = $row_FetchRecordRS['DeliveryAddress'];
    $areaRep =  $row_FetchRecordRS['AreaRepDetails'];
    // $vendor = $row_FetchRecordRS['VendorDetails'];
    $quantity = $row_FetchRecordRS['Quantity'];
    $direct = $row_FetchRecordRS['Direct'];
    $jobID = $row_FetchRecordRS['JobID'];
    $jobName = $row_FetchRecordRS['JobName'];
    $bundlesize = $row_FetchRecordRS['Bundlesize'];
    $bundle = $row_FetchRecordRS['Bundles'];
    $items = $row_FetchRecordRS['Items'];
    $weight = $row_FetchRecordRS['WeightKgs'];
    $totalWeightCol = $row_FetchRecordRS['TotalWeightKgs'];
    $date = date("D M d, Y G:i");

    $total_weight =  $row_FetchRecordRS['FinalWeight'] + $total_weight ;
    echo "Row: " .$row_FetchRecordRS['FinalWeight']. "<br>";
    echo "Total is______  $i : $total_weight <br><br>";  

    $sqlquerytest = "INSERT INTO `GenerateRun`
        (`DateIssued`, `RundateCarrierRunID`, `TimeGenerated`, 
        `CarrierRunID`, `CarrierAreaID`, `DeliveryAddress`, `Postzone`, 
        `CarrierID`, `DeliveryInstruction`, `AreaRepDetails`,  
        `Quantity`, `Direct`, `JobID`, `JobName`, `Bundlesize`,   
        `Bundles`, `Items`, `WeightKgs`, `TotalWeightKgs`,   
        `LodingZoneID`) 
        VALUES 
        ('$dateissued', '$rundateCarrierRun', '$timegenerated',  
         '$carrierID', '$areaID', '$address', '$potzone', '$carr_ID',  
         '$instruction', '$areaRep', '$quantity', '$direct', '$jobID',  
         '$jobName', '$bundlesize', '$bundle', '$items', '$weight',   
        '$totalWeightCol','$i')";

    mysql_select_db($database_callmtlc_SalmatDB, $callmtlc_SalmatDB);
    $ResultUpd1 = mysql_query($sqlquerytest, $callmtlc_SalmatDB) or die(mysql_error()); 

    if ($total_weight >= 1800) {            
        $sqltransitlist = " INSERT INTO `TransitList`(`genID`, `total`) Values ('$i','$total_weight')";
        mysql_select_db($database_callmtlc_SalmatDB, $callmtlc_SalmatDB);
        $ResultUpd3 = mysql_query($sqltransitlist, $callmtlc_SalmatDB) or die(mysql_error());

        $i = $i+1;
        $total_weight = 0;          
    }
} while($row_FetchRecordRS = mysql_fetch_assoc($FetchRecordRS));

1条回答 默认 最新

相关推荐 更多相似问题