我正在尝试更新codeigniter中的表的特定行。但是我的代码会更新所有行。
型号:
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();
}
}控制器:
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;
}并在新页面中显示回显值1。如何更新特定行?为什么$query->num_rows()返回1并更新所有行?
发布于 2014-02-26 15:36:54
尝试使用where条件更新特定记录
$updateData = [
'user_name' => $array['user_name'],
];
$this->db->where('email', $array['email']);
$this->db->update('user', $updateData); 发布于 2014-02-26 15:40:06
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
}控制器
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;
}发布于 2014-02-26 15:44:42
在你的模型中只需使用
$update_data = array('coulmn1_name'=>'value','coulmn2_name'=>'value');
$this->db->where('email', $email);
$this->db->update('my_table', $update_data);https://stackoverflow.com/questions/22034647
复制相似问题