Reputation: 2096
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?
Upvotes: 1
Views: 13118
Reputation: 6344
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);
Upvotes: 10
Reputation: 635
function edit_profile($username, $email){
$this->db->set('username',$username);
$this->db->where('email', $email);
$this->db->update('user');
$result = $this->db->affected_rows();
return $result
}
Controller
public function edit()
{
$name = $this->input->post('user_name', TRUE);
$email=trim($this->session->userdata('email'));
$result = $this->edit_profile_model->edit_profile($name,$email);
echo $result;
}
Upvotes: 2
Reputation: 4829
In your model just use
$update_data = array('coulmn1_name'=>'value','coulmn2_name'=>'value');
$this->db->where('email', $email);
$this->db->update('my_table', $update_data);
Upvotes: 0