Skip to content
Advertisement

Update specific row using codeigniter?

I am trying to update specific row of table in codeigniter. But my code update all rows.

Model:

function edit_profile($array){
        $email=$array['email'];
        $this->db->select("email");
        $this->db->from("user");
        $this->db->where('email', $email);
        $query=$this->db->get();
        if ($query->num_rows() == 1){
        $this->db->update('user',$array);
            return 1;

        }else{
            return $query->num_rows(); 
        }
    }

Controller:

     public function edit()
        {
        $name = $this->input->post('user_name', TRUE);
        $email=trim($this->session->userdata('email'));
        $array = array('user_name'=>$name,'email'=>$email);
        $result = $this->edit_profile_model->edit_profile($array);
        echo $result;
        }

And in new page it show the echo value 1. How to update specific row? why $query->num_rows() return 1 and update all rows?

Advertisement

Answer

Try using a where condition to update a specific record

$updateData = [
   'user_name' => $array['user_name'],
];
$this->db->where('email', $array['email']);
$this->db->update('user', $updateData); 
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement