I am trying to update a specific row of a table in CodeIgniter, but my code update all rows!
Model:
public 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 does $query->num_rows() return 1 and update all rows?
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);