使用codeigniter更新特定行?

时间:2014-02-26 07:28:58

标签: php mysql codeigniter

我正在尝试更新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并更新所有行?

3 个答案:

答案 0 :(得分:8)

尝试使用where条件更新特定记录

$updateData = array(
   'user_name'=>$array['user_name'];
);
$this->db->where('email', $array['email']);
$this->db->update('user', $updateData); 

答案 1 :(得分:1)

    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;
    }

答案 2 :(得分:0)

在您的模型中使用

$update_data = array('coulmn1_name'=>'value','coulmn2_name'=>'value');

$this->db->where('email', $email);
$this->db->update('my_table', $update_data);
相关问题