我的转换表如下
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(); ?>
我的控制器
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列。此外,当我多次插入数据时,它会使用错误的值更新它。
答案 0 :(得分:0)
您的预期查询是:
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'");
}
?>