为什么我的PHP代码中没有发生利息摊销

时间:2016-03-05 09:14:24

标签: php mysql sql

我正在尝试创建一个脚本,使用该贷款摊销公式来分摊我的利息。但我只想分摊利息并将其插入数据库。 我所面临的问题是,我们的兴趣并没有减少。我希望它在数据库中的amo_intr列上生成如下结果

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语法我知道它的描述'。 这是我的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());
}
?>

1 个答案:

答案 0 :(得分: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";
}
?>

得到这样的结果

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计算公式中也有错误:

  • 您需要在循环$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')