PHP MySQL运行减法不起作用

时间:2017-05-06 14:49:12

标签: php mysql

您好我正在尝试支付金额来自所有员工预付款余额并更新交易状态,但它根本不适用于我,我不知道我做错了什么它。实际上,在我的代码中,它将首先选择员工采取的所有提前工资,然后逐个检查剩余余额,并从支付中添加该金额(在其他支付金额上)将被扣除为运行金额)并以状态 1 0

结算每笔交易

我没有重命名变量的问题。

这是MYSQL

+-----------+--------------+--------+------+------------------+---------+
| advanceid | EmployeeCode | Amount | Paid | BalanceRemaining | PayStat |
+-----------+--------------+--------+------+------------------+---------+
|         1 | EMP-40001    |  10000 | 3000 |             7000 |       0 |
|         2 | EMP-40001    |   5000 |    0 |             5000 |       0 |
|         3 | EMP-40001    |  10000 |    0 |            10000 |       0 |
+-----------+--------------+--------+------+------------------+---------+

这是我的PHP

<?php
//Start Updating Advance
$SetAdvanceSalary = $_POST['txtAdvance'];
$total_paying = (float)$SetAdvanceSalary;
$get_advance_row = 0;
$balance_remaining = 0;
$advance_select = "SELECT a.advanceid, a.EmployeeCode, a.Amount AS Amount, a.AmountPaid AS Paid, IFNULL((a.Amount - a.AmountPaid), 0) AS BalanceRemaining, a.PayStat FROM tblemployee_advancesalary AS a WHERE a.EmployeeCode='$SetEmployeeCode' AND a.AmountPaid < a.Amount AND a.PayStat=0 GROUP BY a.advanceid, a.EmployeeCode ORDER BY a.advanceid ASC";
$result_advance = mysqli_query($conn,$advance_select);
$advance_rows = $result_advance->num_rows;
$result_paying = 0;
$print_top = null;
$print_bottom = null;
if ($result_advance->num_rows > 0) {
//Start Selecting Advance Data
//While Loop Start
while($row_advance =  mysqli_fetch_assoc($result_advance)) {
    $balance_put = 0;
    $advance_taken = 0;
    $advance_paid = 0;
    $bal_insert = 0;
    $bal_check = 0;
    $new_balance = 0;
    $new_status = 0;
    $get_advance_row = $row_advance['advanceid'];
    //total to get 10000,
    $advance_taken = (float)$row_advance['Amount']; //10000
    $advance_paid = (float)$row_advance['Paid']; //3000
    $balance_remaining = $advance_taken-$advance_paid;//7000
    $balance_put = $total_paying-$balance_remaining; //12000-7000 = 5000

    $bal_check = $advance_paid+$balance_remaining; // 7000+3000 = 10000
    $total_paying = $total_paying-$balance_remaining;
    if($bal_check == $advance_taken) {
        $new_balance = $bal_check;
        $new_status = 1;
    $total_paying = $total_paying-$balance_remaining;   
    } else {
        $new_balance = $balance_put;
        $new_status = 0;
    $total_paying = $total_paying-$balance_remaining;
    }

//Update Balance Start WHERE Less
$update_advance = "UPDATE tblemployee_advancesalary SET "; 
$update_advance .= "AmountPaid='$new_balance'";
$update_advance .= ", PayStat='$new_status'";
$update_advance .= " WHERE advanceid='$get_advance_row'";
$result_updating = mysqli_query($conn,$update_advance);
    if(!$result_updating) {
        die("SQL Error: " . mysqli_error($conn));
    }
//Update Balance End WHERE Less

} //While Loop End

//End Selecting Advance Data
} else {
continue;
}
//End Updating Advance
?>

1 个答案:

答案 0 :(得分:0)

使用以下代码解决问题,变量变化不大

   <?php
    //Start Updating Advance
$SetAdvanceSalary = $_POST['txtAdvance'];
    $total_paying = (float)$SetAdvanceSalary;
    $get_advance_row = 0;
    $balance_remaining = 0;
    $advance_select = "SELECT a.advanceid, a.EmployeeCode, a.Amount AS Amount, a.AmountPaid AS Paid, IFNULL((a.Amount - a.AmountPaid), 0) AS BalanceRemaining, a.PayStat FROM tblemployee_advancesalary AS a WHERE a.EmployeeCode='$SetEmployeeCode' AND a.AmountPaid < a.Amount AND a.PayStat=0 GROUP BY a.advanceid, a.EmployeeCode ORDER BY a.advanceid ASC";
    $result_advance = mysqli_query($conn,$advance_select);
    $advance_rows = $result_advance->num_rows;
    $result_paying = 0;
    $print_top = null;
    $print_bottom = null;
    if ($result_advance->num_rows > 0) {
    //Start Selecting Advance Data
    //While Loop Start
    while($row_advance =  mysqli_fetch_assoc($result_advance)) {
        $balance_put = 0;
        $balance_less = 0;
        $advance_taken = 0;
        $advance_paid = 0;
        $bal_insert = 0;
        $bal_check = 0;
        $new_balance = 0;
        $new_status = 0;
        $get_advance_row = $row_advance['advanceid'];
        //total to get 10000,
        $advance_taken = (float)$row_advance['Amount']; //10000
        $advance_paid = (float)$row_advance['Paid']; //3000
        $balance_remaining = $advance_taken-$advance_paid;//7000

        if($total_paying >= $balance_remaining) {
            $balance_put = $advance_paid+$balance_remaining; //this will settle if equal
            $total_paying=$total_paying-$balance_remaining;
            $new_balance = $balance_put;
            $new_status = 1;
                //Update Balance Start
                $update_advance = "UPDATE tblemployee_advancesalary SET "; 
                $update_advance .= "AmountPaid='$new_balance'";
                $update_advance .= ", PayStat='$new_status'";
                $update_advance .= " WHERE advanceid='$get_advance_row'";
                $result_updating = mysqli_query($conn,$update_advance);
                    if(!$result_updating) {
                        die("SQL Error: " . mysqli_error($conn));
                    }
                //Update Balance End

        } elseif($total_paying < $balance_remaining && $total_paying>0) {
                $balance_less = $total_paying; // this will not equal
                $total_paying=$total_paying-$balance_remaining;
            $new_balance = $balance_less;
            $new_status = 0;
                //Update Balance Start
                $update_advance = "UPDATE tblemployee_advancesalary SET "; 
                $update_advance .= "AmountPaid='$new_balance'";
                $update_advance .= ", PayStat='$new_status'";
                $update_advance .= " WHERE advanceid='$get_advance_row'";
                $result_updating = mysqli_query($conn,$update_advance);
                    if(!$result_updating) {
                        die("SQL Error: " . mysqli_error($conn));
                    }
                //Update Balance End

        } elseif ($total_paying <= 0) {
            continue;
        }else {
            continue;
        }
    } //While Loop End

    //End Selecting Advance Data
    } else {
    continue;
    }
    //End Updating Advance
    ?>