PHP:从另一个表中获取数据时更新产品表数量

时间:2018-02-12 14:31:00

标签: php mysql

我在从invoiceitem表更新提取的数据时遇到问题,以更新产品表quantity

此问题只是我的循环中第一条正在更新的记录。

请帮我更新数据库中的多个项目值。

这是我的代码:

<?php



$stuid = $_GET['id'];
global $con;

                    $stu_query = "SELECT * FROM invoiceorder INNER JOIN invoiceorderitem ON invoiceorder.InvoId = invoiceorderitem.InvoId WHERE invoiceorder.InvoId ='$stuid'";

                    $stu_run = mysqli_query($con,$stu_query);


                    while($sorows = mysqli_fetch_array($stu_run)){
                        $Date = $sorows['Date'];
                        $SalesPerson = $sorows['SalesPerson'];
                        $Telephone = $sorows['Telephone'];                              
                        $Customer = $sorows['Customer'];                                
                        $CurrentPayment = $sorows['CurrentPayment'];                                
                        $Balance = $sorows['Balance'];                              

                        $Item_Id = $sorows['Item_Id'];
                        $ItemDescription = $sorows['ItemDescription'];
                        $UnitPrice = $sorows['UnitPrice'];
                        $Quantity = $sorows['Quantity'];
                        $SubTotal = $sorows['SubTotal'];
                        $Total = $sorows['Total'];

                     $sea_res = mysqli_query($con, "SELECT * FROM bincard WHERE BinId = $Item_Id");

                                        while ($resresults = $sea_res->fetch_assoc()){

                                            $BinId = $resresults['BinId'];
                                            $ItemName = $resresults['ItemName'];
                                            $QtyBalance = $resresults['QtyBalance'];
                                            $QtyIssued = $resresults['QtyIssued'];

                                            //chech this
                                            $newqty=0;
                                            $isuqty=0;

                                            $newqty =$QtyBalance - $Quantity;
                                            $isuqty = $QtyIssued + $Quantity;
                                            $insert_query = "UPDATE bincard SET QtyBalance='$newqty',QtyIssued='$isuqty' WHERE BinId = $Item_Id";
                                        }
                                        if ($con->query($insert_query) === TRUE) 
                        {
                            echo"<script>alert(' Updated Successfully')</script>";
                            echo"<script>window.open('invoiceprint.php','_self')</script>";
                        } else 
                        {
                            echo "Error: " . $insert_query . "<br>" . $con->error;
                        }



                        $con->close();
                    }







?>

1 个答案:

答案 0 :(得分:1)

if ($con->query($insert_query) === TRUE)放入while循环中。如果它只更新一条记录,那么$sea_res->fetch_assoc()只有一条记录。

while ($resresults = $sea_res->fetch_assoc()){

    $BinId = $resresults['BinId'];
    $ItemName = $resresults['ItemName'];
    $QtyBalance = $resresults['QtyBalance'];
    $QtyIssued = $resresults['QtyIssued'];

    //chech this
    $newqty=0;
    $isuqty=0;

    $newqty =$QtyBalance - $Quantity;
    $isuqty = $QtyIssued + $Quantity;
    $insert_query = "UPDATE bincard SET QtyBalance='$newqty',QtyIssued='$isuqty' WHERE BinId = $Item_Id";

    if ($con->query($insert_query) === TRUE) 
    {
        echo"<script>alert(' Updated Successfully')</script>";
        echo"<script>window.open('invoiceprint.php','_self')</script>";
    } else{
        echo "Error: " . $insert_query . "<br>" . $con->error;
    }
}