2015-11-17 16:50
浏览 69


I am building a system where i need to update the value in the database field but before doing that i need to take the current value in the database then adding it to the current value the following is a code from the controller.

public function transfer_amount(){

    $email  =  $this->input->post('view');


    $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){






then the code in the module that fetch the current balance from the database is as following.

 function fetch_balance($email){

    $this->db->where('email', $email);

    $query = $this->db->get();
    $result = $query->result();

    return $result;

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • douxian1892 2015-11-17 22:14

    not sure but look at this part - don't call it $data because i think thats messing you up later

    for example call it $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 

    getting the balance should be wrapped in an if in case it fails

    if( ! $data['user_balance'] = $this->user_model->fetch_balance($email) )
       $this->_showNoUserFor($email) ; 

    another suggestion:

    $email  =  $this->input->post('view');

    validate the email first before sending it to your database table. codeigniter form validation library works really well.


    EDIT ok this part

     $data['total'] =   $this->math->add($data['balance'],$data['user_balance']);

    means that you have a model called math with a method called add() so if you don't have that you would just use php math which is very simple

    $data['total'] = $data['balance'] + $data['user_balance'] ; 

    of course this assumes everything has been validated first so you are actually adding together two numbers.

    解决 无用
    打赏 举报

相关推荐 更多相似问题