gpt4 book ai didi

php - 多行的总和并使用 Codeigniter 插入另一列不起作用

转载 作者:行者123 更新时间:2023-11-30 22:08:45 25 4
gpt4 key购买 nike

我的交易表如下

id      acount              dr                 cr               bal
1 Brack Bank 0.00 5000.00 0.00
2 Uttora Bank 0.00 1000.00 5000.00
3 FC Bank 0.00 3000.00 6000.00
4 purchase 4000.00 0.00
5 sallary 3000.00 0.00

我想让我的交易表如下
id      acount             dr                 cr              bal
1 Brack Bank 0.00 5000.00 5000.00
2 Uttora Bank 0.00 1000.00 6000.00
3 FC Bank 0.00 3000.00 9000.00
4 purchase 4000.00 0.00 5000.00
5 sallary 3000.00 0.00 2000.00

我的观点
<?php echo form_open('transaction/add',array("class"=>"form-horizontal")); ?>

<?php $p = $sum[0]->cr; ?><br/>
<?php $q = $sum[0]->dr; ?><br/>

<input type="hidden" value="<?php echo $p - $q ?>" name="calculation">

<input type="text" name="account" value="<?php echo $this->input->post('account'); ?>" class="form-control" id="account" />

<button type="submit" class="btn btn-success">Save</button>

<?php echo form_close(); ?>

我的 Controller
   function add()
{
if(isset($_POST) && count($_POST) > 0)
{
$params = array(
'account' => $this->input->post('account'),
'bal' => $this->input->post('calculation'),
);

$accounts_transaction_id = $this->Accounts_transaction_model->update_transactions_balance($params);

redirect('transaction/index');

}else{
$this->load->view('transaction/new_deposit');
}
}

我的模型
   function update_transactions_balance($params)
{

$account = $this->input->post('account');
$bal = $this->input->post('calculation');
$this->db->set('bal', "bal+$bal", FALSE);
$this->db->where('account', "$account");
$this->db->update('accounts_transactions');
}

我想在提交新数据时更新每一行。问题是它没有在第一次插入数据时更新我的​​ bal 列。此外,当我多次插入数据时,它会用错误的值更新它。

Current data

最佳答案

您的预期查询是:

UPDATE accounts_transactions SET bal = bal+$bal WHERE account = '$account';

所以,
<?php
function update_transactions_balance($params)
{
$account = $this->input->post('account');
$bal = $this->input->post('calculation');
$this->db->query("UPDATE accounts_transactions SET bal = bal+$bal WHERE account = '$account'");
}
?>

关于php - 多行的总和并使用 Codeigniter 插入另一列不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40807353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com