删除在while循环中无法正常工作

时间:2017-01-20 15:09:29

标签: php mysql

这里我试图从详细信息表中删除这里是我的代码的一个问题,如果详细信息表有三条记录,而不是从详细信息表中删除一条记录。而且它不会影响股票表中的其他两条记录。  只有一个第一个记录此代码正常工作后,它不适用于详细信息表中的删除查询,不会在库存表中生效...

<?php
include("include/config.inc.php");


$purchaseMasterId = isset($_REQUEST['purchaseMasterId']) ? $_REQUEST['purchaseMasterId'] : 0;

if($purchaseMasterId > 0) {
    $k = 0;
    $selectMaster = "SELECT purchasedetail.purchaseMasterId, colorId, 
                            totalkg, purchaseDetailId, partyId
                    FROM purchasedetail  
                        JOIN purchasemaster ON 
                            purchaseMaster.purchaseMasterId = purchasedetail.purchaseMasterId
                    WHERE purchasedetail.purchaseMasterId  = ".$_REQUEST['purchaseMasterId'];

    $selectMasterRes = mysql_query($selectMaster);
    while($purRow = mysql_fetch_array($selectMasterRes)) {
        $purchaseDetailId   = $purRow['purchaseDetailId'];
        $purchaseMasterId   = $purRow['purchaseMasterId'];
        $colorId            = $purRow['colorId'];
        $totalkg            = $purRow['totalkg'];
        $partyId            = $purRow['partyId'];

        $select = "SELECT qty
                    FROM stock
                    WHERE partyId = ".$partyId."
                    AND colorId = ".$colorId;

        $selectRes = mysql_query($select);
        if($stockRow = mysql_fetch_array($selectRes)) {
            $current = $stockRow['qty'];
        }

        $updateStock = "UPDATE stock
                        SET qty = ".$current." - ".$totalkg."
                    WHERE partyId = ".$partyId."
                      AND colorId = ".$colorId;

        $updateStockRes = mysql_query($updateStock) or die(mysql_error());
        if($updateStockRes) {
            $deleteDt = "DELETE FROM purchasedetail 
                        WHERE purchaseDetailId  = ".$purchaseDetailId;

            $deleteRes = mysql_query($deleteDt);
            if($deleteRes){
                $deleteMst = "DELETE FROM purchasemaster 
                             WHERE purchaseMasterId  = ".$_REQUEST['purchaseMasterId'];

                $deleteMstRes = mysql_query($deleteMst) or die(mysql_error());
                if(!$deleteMstRes) {
                    echo "Purchase Master Delete Fail";
                } else {
                    header("Location:purdetail.php");
                    exit();
                }
            }
        }
    }
} 
?>

1 个答案:

答案 0 :(得分:0)

这是我更新的代码,包含完美的代码

   <?php
    include("include/config.inc.php");

    $purchaseMasterId = isset($_REQUEST['purchaseMasterId']) ? $_REQUEST['purchaseMasterId'] : 0;

    if($purchaseMasterId > 0)
    {
        $k = 0;
        $selectMaster = "SELECT purchasedetail.purchaseMasterId,colorId,totalkg,purchaseDetailId,partyId
                            FROM purchasedetail  
                        JOIN purchasemaster ON purchasemaster.purchaseMasterId = purchasedetail.purchaseMasterId
                             WHERE purchasedetail.purchaseMasterId  = ".$_REQUEST['purchaseMasterId'];
        $selectMasterRes = mysql_query($selectMaster) or die(mysql_error());
        while($purRow = mysql_fetch_array($selectMasterRes))
        {
          $purchaseDetailId   = $purRow['purchaseDetailId'];
          $purchaseMasterId   = $purRow['purchaseMasterId'];
          $colorId            = $purRow['colorId'];
          $totalkg            = $purRow['totalkg'];
          $partyId            = $purRow['partyId'];



            $select = "SELECT qty
                         FROM stock
                       WHERE partyId = ".$partyId."
                       AND colorId = ".$colorId;
            $selectRes = mysql_query($select);
            if($stockRow = mysql_fetch_array($selectRes))
            {
              $current = $stockRow['qty'];
            }

            $updateStock = "UPDATE stock
                              SET qty = ".$current." - ".$totalkg."
                              WHERE partyId = ".$partyId."
                            AND colorId = ".$colorId;
            $updateStockRes = mysql_query($updateStock) or die(mysql_error());
            if($updateStockRes)
            {
              $deleteDt = "DELETE FROM purchasedetail 
                             WHERE purchaseDetailId  = ".$purchaseDetailId;
              $deleteRes = mysql_query($deleteDt);
              if($deleteRes)
              {
                $selectid = "SELECT purchaseDetailId
                                FROM purchasedetail
                             WHERE purchaseMasterId  = ".$purchaseMasterId;
                $selectidRes = mysql_query($selectid);
                if(mysql_affected_rows() == 0)
                {
                  $mst = "DELETE FROM purchasemaster
                             WHERE purchaseMasterId  = ".$purchaseMasterId;
                  $mstRes = mysql_query($mst) or die(mysql_error());;   
                  if($mstRes)
                  {
                    header("Location:purdetail.php");
                    exit();
                  }
                  else
                  {

                  }
                }
              }
            }
        }
    } 
    ?>