如何根据给定条件将数据插入mysql?

时间:2016-08-29 13:58:44

标签: php mysql

好吧,如果客户支付他每月运行的账单,我会将数据插入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'  ) 
"); 
}

1 个答案:

答案 0 :(得分:0)

试试这个

 $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;
    }



?>