shibbir ahmed shibbir ahmed - 3 months ago 10
MySQL Question

How to insert data to mysql based on given condition?

Well, I am inserting data to

client_pay_bill
table if client pay his running monthly bill.

So for e.g;

Today is 2016-08-29.

If Client bill is
700
and he paid more than his monthly bill e.g
1800
then I want to divided this
1800 / 700
. So
pay_amount
,
bill_month
and
due_amount
column data will be insert 3 times in following way :

table : clients_pay_bill

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


Otherwise If he paid only monthly bill
700
or less then
700
I want to run my
else
statement.

So for that I can't determine how the query should look like. Bellow is my code (Not finished)

$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' )
");
}

Answer

Try This

 $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 as to DIVIDE money

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



?>