Php 使用循环更新数据库表

Php 使用循环更新数据库表,php,mysql,database,Php,Mysql,Database,单击“我的付款单”按钮时,我想更新“已付款金额”字段。单击“付款”字段时,下面的表格数据已加载,但未加载“已付款金额” `SI_ID is` the Auto Increment Field `SI_Reg_No` i the student registration number `SI_Ins_NO` is the installment number `SI_Due_Date` is the due date of that installment `SI_Paid_Amount`

单击“我的付款单”按钮时,我想更新“已付款金额”字段。单击“付款”字段时,下面的表格数据已加载,但未加载“已付款金额”

`SI_ID is` the Auto Increment Field
`SI_Reg_No` i the student registration number 
`SI_Ins_NO` is the installment number 
`SI_Due_Date` is the due date of that installment 
`SI_Paid_Amount` is the paid amount of each installment
如果SI_已付款,则应更新金额,如下所示

从第一个SI_Ins_No开始,如果SI_Ins_amount大于$amount,则应更新SI_Paid_amount字段,并应停止循环。像吼叫


如果SI_in_金额小于发送金额,则应更新第二个SI_in_No related SI_Paid_Amount列。同时,在将数据发送到SI_Paid_Amount时,我们希望根据SI_in_No检查以前的SI_Paid_金额。因此,我的问题是如何使用php实现这一点

我和这个家伙聊过,知道他在找什么。基本上,这是一张供学生分期付款的桌子,但他们可以批量或其他方式付款

例如: 所以我欠了15000、10000和5000三期。我支付17000英镑,并根据ASC订单中的SIU INSU No从我的3期付款中扣除15000英镑。因此,它将在SIU ID 1中增加5000,然后在SIU ID 2中增加2000

我写了一个小的解决方案来帮助你

$amount = 5000.00 // this is what i send to the db for update `SI_Paid_Amount`
请随时改进,因为这只是一个快速粗糙的方法

你可以测试一下


您需要添加自己的内容,如检查金额是否为正数,确保他们不会支付太多等。

您能否重新编写/澄清您的问题/问题,因为事实证明这很难理解。您使用哪种数据库?好的。所以我不明白你想要什么。你能详细解释一下你的情况吗?诸如此类,必须准备好的陈述,诸如此类。
// $rows = "SELECT *, (`SI_Ins_Amount` - `SI_Paid_Amount`) as owed FROM `student_installments` WHERE (`SI_Ins_Amount` + `SI_Paid_Amount`) != 0 AND `SI_Reg_No` = 'COL/A-000041' ORDER BY `SI_Ins_NO` ASC";

$rows = array();
$rows[0]['SI_ID'] = 1;
$rows[0]['SI_Reg_no'] = 'COL/A-000041';
$rows[0]['SI_Ins_Amount'] = '15000';
$rows[0]['SI_Paid_Amount'] = '0';
$rows[0]['owed'] = '15000';
$rows[1]['SI_Ins_NO'] = '1';

$rows[1]['SI_ID'] = 2;
$rows[1]['SI_Reg_no'] = 'COL/A-000041';
$rows[1]['SI_Ins_Amount'] = '10000';
$rows[1]['SI_Paid_Amount'] = '0';
$rows[1]['owed'] = '10000';
$rows[1]['SI_Ins_NO'] = '2';

$rows[2]['SI_ID'] = 2;
$rows[2]['SI_Reg_no'] = 'COL/A-000041';
$rows[2]['SI_Ins_Amount'] = '5000';
$rows[2]['SI_Paid_Amount'] = '0';
$rows[2]['owed'] = '5000';
$rows[1]['SI_Ins_NO'] = '3';

$amount = 27000;
foreach($rows as $r) {
    // There's money left
    if($r['owed'] - $amount < 0) {
        $paying = $r['owed'];
        $amount -= $r['owed'];
    } else {
        // No money left
        $paying = ($r['SI_Paid_Amount' + $amount);
        $amount -= $paying;
    }

    // If there's money left
    if($paying > 0) { 
       echo "UPDATE `student_installments` SET `SI_Paid_Amount` = '".$paying."' WHERE `SI_ID` = '".$r['SI_ID']."' \n";
   }
}