好吧,如果客户支付每月运行账单,我将数据插入client_pay_bill表。

因此例如

今天是2016-08-29

如果客户帐单是700,并且他支付的费用超过了每月帐单,例如1800,那么我想将这笔1800 / 700分摊。所以pay_amountbill_monthdue_amount列数据将按以下方式插入3次:

表格:clients_pay_bill

pay_amount    bill_month     due_amount
=======================================
700           2016-08-29     0
700           2016-09-29     0
400           2016-10-29     0


否则,如果他仅支付700或少于700的每月帐单,那么我要运行else语句。

因此,我无法确定查询的外观。波纹管是我的代码(未完成)

$entry_date = time();

if($advance_amount >  $monthly_bill) {

    $counting_month = round($advance_amount / $monthly_bill);
    $advance_amount .= $ad;

    for ($x=0; $x<=$counting_month; $x++) {
        $r = $advance_amount - $monthly_bill;
        $ad = $r;

        $insert_to_month = mysqli_query($conn, "INSERT INTO
        clients_pay_bill (client_id, pay_amount, bill_month, receipt_no,
        entry_date, uid, remark) VALUES('$client_id', '', '', '', '', '',
        '' ) ");
    }
} else {
    $insert = mysqli_query($conn, "INSERT INTO clients_pay_bill (client_id,
    pay_amount, discount_amount, due_amount, advance_amount, bill_month,
    receipt_no, entry_date, is_paid, uid, remark) VALUES('$client_id',
   '$paid_amount', '$discount_amount', '$due_amount', '$advance_amount',
   '$bill_month', '$receipt_no', '$entry_date', '1', '$uid', '$remark'  )
");
}

最佳答案

尝试这个

 $entry_date = date("Y-m-d");

if($advance_amount >  $monthly_bill) {

$counting_month = round($advance_amount / $monthly_bill);
$advance_amount .= $ad;

for ($x=0; $x<=$counting_month; $x++) { //This loop will run for 700 700
    $r = $advance_amount - $monthly_bill;
    $ad = $r;
    $forOdNextMonth= date('m', strtotime("+".$x." month", strtotime($entry_date )));
    $insert_to_month = mysqli_query($conn, "INSERT INTO
    clients_pay_bill (client_id, pay_amount, bill_month, receipt_no,
    entry_date, uid, remark) VALUES('$client_id', '$monthly_bill;', '$forOdNextMonth', '$receipt_no', '$entry_date', '$uid', '$remark' ) ");
}if($r>0){ //This loop will run for remaining amount 400 as amount will be 1800

$forOdNextMonth= date('m', strtotime("+".($x+1)." month", strtotime($entry_date )));
 $insert_to_month = mysqli_query($conn, "INSERT INTO
    clients_pay_bill (client_id, pay_amount, bill_month, receipt_no,
    entry_date, uid, remark) VALUES('$client_id', '$r', '$forOdNextMonth', '$receipt_no', '$entry_date', '$uid', '$remark' ) ");
 }
 } else {
$insert = mysqli_query($conn, "INSERT INTO clients_pay_bill (client_id,
pay_amount, discount_amount, due_amount, advance_amount, bill_month,
receipt_no, entry_date, is_paid, uid, remark) VALUES('$client_id',
'$paid_amount', '$discount_amount', '$due_amount', '$advance_amount',
'$bill_month', '$receipt_no', '$entry_date', '1', '$uid', '$remark'  )
");
}


关于分钱的TESTCODE

<?php
$amount=1800;
$monthlybill=700;
for($i=0;$i<$amount/$monthlybill;$i++)
{
echo"paid for ".($i+1)." month =".$monthlybill."<br>";
$amount-=$monthlybill;
}
if($amount>0){

    echo"paid for ".($i+1)." month =".$amount;
    }



?>

关于php - 如何根据给定条件向mysql插入数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39208129/

10-10 11:32