我正在构建一个系统,在这个系统中,我需要更新数据库字段中的值,但在此之前,我需要获取数据库中的当前值,然后将其添加到当前值,下面是来自控制器的代码。
public function transfer_amount(){
$email = $this->input->post('view');
$this->load->model('user_model');
$data['user_balance'] = $this->user_model->fetch_balance($email);
$data['balance'] = $this->input->post('amount');
$data['total'] = $this->math->add($data['balance'],$data['user_balance']);
$data = array(
'balance' => $data['total'],
);
if($this->user_model->transfer_amount($data,$email)== true){
$this->load->view('layer_service/success',$data);
}
else
{
$this->load->view('layer_service/unsuccessfull',$data);
}
}}
然后从数据库获取当前余额的模块中的代码如下所示。
function fetch_balance($email){
$this->db->select('balance');
$this->db->from('tbl_users');
$this->db->where('email', $email);
$query = $this->db->get();
$result = $query->result();
return $result;
}发布于 2015-11-17 22:14:49
不确定,但是看看这部分--别叫它$data,因为我想以后会把你搞砸的。
例如,叫它$balance
$balance = array(
'balance' => $data['total'],
);
// pass the $balance to your model method
if($this->user_model->transfer_amount($balance,$email) == true){
// keeping $data here to pass to the view
$this->load->view('layer_service/success',$data);
}获得余额时,应将其包装在if中,以防失败。
if( ! $data['user_balance'] = $this->user_model->fetch_balance($email) )
{
$this->_showNoUserFor($email) ;
} 另一项建议:
$email = $this->input->post('view');在将电子邮件发送到数据库表之前先验证它。代码点火器表单验证库工作得很好。
======
编辑好这部分
$data['total'] = $this->math->add($data['balance'],$data['user_balance']);意味着你有一个叫做数学的模型,它有一个叫做add()的方法,所以如果你没有,你只需要使用php数学,这是非常简单的
$data['total'] = $data['balance'] + $data['user_balance'] ; 当然,这假设一切都已经过验证,所以实际上是将两个数字相加在一起。
https://stackoverflow.com/questions/33762597
复制相似问题