How to update existing record using codeIgniter

2019-08-08 04:04发布

问题:

I have a table like this: id, image, user_id.

The user_id is saved from the session. The same user wants to update his profile picture means, how it will update?

This Is Controller Function

public function save() {

   $session_data = $this->session->userdata('logged_in');
   $uid= $session_data['id'];

   //print_r($uid);exit;

   $url = $this->do_upload();
   $this->load->model('user_model');
   $this->user_model->save($url,$uid);

   echo '<script>alert("You Have Successfully Uploaded your Profile Picture!");</script>';

   redirect('/home/');
}

This Is Model

public function save($url, $uid){
    $this->db->set('image', $url);
    $this->db->set('user_id', $uid);
    $this->db->insert('tbl');
    return true;
}

How can I update this query?

回答1:

You can do something like the following:

$upd_data = array(
   'image' = $url,
);

$this->db->where('user_id', $uid)
         ->update('tbl', $upd_data);


标签: codeigniter