我不知道我的代码 php 出了什么问题,如果购物车中只有 1 件商品正常工作,但如果许多商品无法正常工作


I dont know whats wrong in my code php If only 1 item in cart works properly but if many not working properly

>我的代码找不到逻辑错误我希望 for 循环在扣除之前检查所有数量是否可用,这样我就不会得到负数量

    if(isset($_POST['addtype'])){
        $addedby;
        $tchange;
        $vatta;
        $vatt;
        $fdiscount;
        $sum;
        $cash;
        $custname=$_POST['custname'];
        $custadd=$_POST['custadd'];
        $discount1=($fdiscount/100);
            $salesdate=date("Y-m-d H:i:s");
            $disc = ($sum * $discount1);
        $discamount =($sum - $disc);
            if($cash < $discamount){
            echo "<script> alert('Please Input Cash!');</script>";  

            }
            else{
                $tchange = ($cash - $discamount);
                $counter = 0;
                $salesbarcode = null;
                $salesquantity = null;
                $sales = "SELECT  * FROM salesitem WHERE transno='$transacnum'";
                $squery =mysql_query($sales) or die ("ERROR".mysql_error());
                while ($sarray = mysql_fetch_array($squery))
                {
                $salesbarcode[$counter] = $sarray['barcode'];
                $salesquantity[$counter] = $sarray['quantity'];
                $counter++;
                }

这是我的 for 循环,因此它将检查购物车数量<库存数量>

for ($i = 0; $i < $counter; $i++)
                {
                $salesbarcode[$i];
                $salesquantity[$i];
                $salebcode=$salesbarcode[$i];
                $selectitem = "SELECT * FROM productinfo WHERE barcode='$salebcode'";
                $stquery = mysql_query($selectitem) or die ("ERROR".mysql_error());
                $strow = mysql_fetch_array($stquery);
                $itemstock = $strow['qty'];
                $itemname= $strow['prod_name'];
                if($itemstock < $salesquantity[$i] )
                {
                    echo "<script type='text/javascript'>alert('Insufficient Stock for Item $itemname')</script>";
                    echo "<script type='text/javascript'>  window.location='pos1.php'</script>";
                }
                else{           
                            $selectqty = "SELECT  * FROM salesitem WHERE transno='$transacnum'";
                            $resultqty = mysql_query($selectqty) or trigger_error("SQL", E_USER_ERROR);
                            if ($resultqty)
                            {
                                    $qty=0;
                                    $count = 0; 
                                    $posbcode=null;
                                    $posiqty=null;
                                    $a=0;
                                while ($list = mysql_fetch_array($resultqty)) 
                                {
                                        $posbcode[$count] = $list['barcode'] ; 
                                        $posiqty[$count] = $list['quantity'] ;
                                        $count++;
                                } 
                                for ($i = 0; $i < $count; $i++)
                                    {
                                            $posbcode[$i];  
                                            $posiqty[$i]; 
                                            $pQty=$posiqty[$i] ;
                                            $ino=$posbcode[$i];
                                            $select="SELECT * from productinfo Where barcode='$ino'";
                                            $res=mysql_query($select);
                                            while($r=mysql_fetch_array($res))
                                            {
                                            $stkQty=$r['qty'];
                                            $remQty= $stkQty-$pQty;
                                            $update="UPDATE productinfo SET qty='$remQty' Where barcode='$ino'";
                                            mysql_query($update) or die("Error: ".mysql_error());
                                            }
                                    }   
                            }
                                             $insert=mysql_query("INSERT INTO sales(salestransac ,salesdate,total,cash,discount,tchange,discamount,vatsales,vatamount,custname,custadd,AddedBy) VALUES ('$transacnum','$salesdate','$sum','$cash','$fdiscount','$tchange','$discamount','$vatt','$vatta','$custname','$custadd','$addedby')")or die (mysql_error());
                                                if($insert){
                                                    echo "<script>alert('Transaction Complete!');window.location.href='posrec.php';</script>";}

            }

你在循环中使用$i

for ($i = 0; $i < $counter; $i++) on line 1

for ($i = 0; $i < $count; $i++) on line 32

例如,将 2. 一更改为$t