2016-03-05 46 views
-1

我正在嘗試創建一個腳本,使用該貸款攤銷公式分攤我的利息。但我只想分攤利息並將其插入數據庫。 我面臨的問題是我的程序所面臨的問題是,利息並沒有減少,因爲它假設要做。我希望它產生類似下面的結果就amo_intr的列數據庫爲什麼這種利息攤銷不是發生在我的PHP代碼

amo_intr 
 
12.00 
 
11.50 
 
10.95 
 
10.32 
 
9.62 
 
8.84 
 
7.96 
 
6.98 
 
5.88 
 
4.65 
 
3.27 
 
1.73

但相反我得到的結果類似的圖像:databaseresult 我需要幫助不關於MySQL的句法評論我知道它的depricated'。 THIS IS MY PHP代碼值插入數據庫:

<?php 
 
include("connection/db_con.php"); 
 
$nn=11; 
 
$install=16.14; 
 
$edat='2016-03-15'; 
 
$amt=100; 
 
$i=0.12; 
 
$actual_l='L8080'; 
 
$ecn='R555555'; 
 
$amo_intr=$amt*$i; 
 
mysql_query("insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('$edat','$amo_intr','$actual_l','$ecn','$amt')") or die(mysql_error()); 
 
$lt=0; 
 
while($lt<$nn) 
 
{ 
 
$lt++; 
 
$balance=$amt-($install-$amo_intr); 
 
$amo_intr=$balance*$i; 
 
$otherdat = strtotime(date("Y-m-d", strtotime($edat)) . " +".$lt." month"); 
 
$otherdat = date("Y-m-d",$otherdat); 
 
mysql_query("insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('$otherdat','$amo_intr','$actual_l','$ecn','$amt')") or die(mysql_error()); 
 
} 
 
?>

+0

你是什麼'amo_intr'列的數據類型? – eggyal

+0

哦,順便說一句 - 「*不要評論關於mysql語法,我知道它的depricated *」......它簡單地將mysql換成mysqli比輸入那9個單詞更容易。 – eggyal

+0

amo_intr是十進制的 – Leereal

回答

0

它看起來像你的領域amo_intr的類型爲int。

我嘗試用這個PHP腳本

<?php 
date_default_timezone_set('America/Los_Angeles'); 

$nn=11; 
$install=16.14; 
$edat='2016-03-15'; 
$amt=100; 
$i=0.12; 
$actual_l='L8080'; 
$ecn='R555555'; 
$amo_intr=$amt*$i; 
echo ("insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('$edat','".(int)$amo_intr."','$actual_l','$ecn','$amt')")."\n"; 
$lt=0; 
while($lt<$nn) 
{ 
    $lt++; 
    $balance=$amt-($install-$amo_intr); 
    $amo_intr=$balance*$i; 
    $otherdat = strtotime(date("Y-m-d", strtotime($edat)) . " +".$lt." month"); 
    $otherdat = date("Y-m-d",$otherdat); 
    echo ("insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('$otherdat','".(int)$amo_intr."','$actual_l','$ecn','$amt')")."\n"; 
} 
?> 

到simultate,並得到這樣的結果

insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-03-15','12','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-04-15','11','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-05-15','11','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-06-15','11','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-07-15','11','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-08-15','11','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-09-15','11','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-10-15','11','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-11-15','11','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-12-15','11','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2017-01-15','11','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2017-02-15','11','L8080','R555555','100') 

也有你的$公式中的錯誤amo_intr計算:

  • 你需要在while循環前初始化餘額$balance = $amt;
  • 並計算內部循環餘額$balance-=($install-$amo_intr);

工作代碼爲你:

<?php 
date_default_timezone_set('America/Los_Angeles'); 

$nn=11; 
$install=16.14; 
$edat='2016-03-15'; 
$amt=100; 
$i=0.12; 
$actual_l='L8080'; 
$ecn='R555555'; 
$amo_intr=$amt*$i; 
echo ("insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('$edat','".$amo_intr."','$actual_l','$ecn','$amt')")."\n"; 

$balance = $amt; 
$lt=0; 
while($lt<$nn) 
{ 
    $lt++; 
    $balance-=($install-$amo_intr); 
    $amo_intr=$balance*$i; 
    $otherdat = strtotime(date("Y-m-d", strtotime($edat)) . " +".$lt." month"); 
    $otherdat = date("Y-m-d",$otherdat); 
    echo ("insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('$otherdat','".$amo_intr."','$actual_l','$ecn','$amt')")."\n"; 
} 
?> 

隨着輸出

insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-03-15','12','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-04-15','11.5032','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-05-15','10.946784','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-06-15','10.32359808','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-07-15','9.6256298496','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-08-15','8.843905431552','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-09-15','7.9683740833382','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-10-15','6.9877789733388','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-11-15','5.8895124501395','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-12-15','4.6594539441562','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2017-01-15','3.281788417455','L8080','R555555','100') 
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2017-02-15','1.7388030275496','L8080','R555555','100') 
+0

謝謝你的工作完美。你做的非常出色。再次感謝 – Leereal