duanfu3884 2015-11-17 15:04
浏览 49
已采纳

使用Codeigniter在mysql数据库中更新

I have been trying to do the update the record in my database but i am getting the echo that the action have not being updated all i want is to update balance field in the database! The following is the code in the controller where i take value that i have passed from ajax and i am passing the email and data to model. Controller.

 public function transfer_amount(){


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



    $data = array(

            'balance' => $this->input->post('amount'),

    );

    $this->load->model('user_model');


    if($this->user_model->transfer_amount($data,$email)){


        echo'unsuccessfull';


    }
    else
    {

        $this->load->view('layer_service/success',$data);

    }



}

The Model which is supposed to update the field of the database is as following.

   function transfer_amount($data,$email){



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

    $this->db->update('tbl_users', $data);

        // Code here after successful insert
     // to the controller

    return true;


}
  • 写回答

1条回答 默认 最新

  • doudiemei2013 2015-11-17 16:06
    关注

    Based on your above comment you need to trim the whitespace from the email.

    function transfer_amount($data, $email) {
    
      $this->db->update('tbl_users', $data, array(
        'email' => trim($email),
      ));
    
      // Code here after successful insert
      // to the controller
    
      return true;
    }
    

    Also in your controller you will want to replace this line

    if($this->user_model->transfer_amount($data,$email)){
    

    with

    if( ! $this->user_model->transfer_amount($data,$email)) {
    

    As your model returns TRUE on success but your controller is expecting FALSE on success.

    You can also check that your update was successful and did actually change some rows in the database using a simple if statement such as

    if(0 >= $this->db->affected_rows()) {
      // update unsuccessful
    }
    

    Update

    How to add to the current balance in MySQL. You can simply add to the value which is already in the database.

    $data = array(
      'balance' => 'balance + ' . $this->input->post('amount'),
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据