笨。更新id问题

时间:2011-03-20 23:35:58

标签: codeigniter where

我正在尝试更新联系人,只根据ID更新联系人。

型号:

function updateContact() {

    $data = array(
        'title' => $this->input->post('title'),
        'first_name' => $this->input->post('first_name'),
        'surname' => $this->input->post('surname'),
        'phone_number' => $this->input->post('phone_number'),
        'fax' => $this->input->post('fax'),
        'email' => $this->input->post('email')                      
    );

    $this->db->where('id', $this->uri->segment(3));
    $this->db->update('contacts', $data);


}   

控制器:

    function update() { 

    $this->load->model('league/add_contact_model');
    $data['rows'] = $this->add_contact_model->viewContact();

    // field name, error message, validation rules
    $this->form_validation->set_rules('title', 'Title', 'trim|required');
    $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
    $this->form_validation->set_rules('surname', 'Surname Name', 'trim|required');
    $this->form_validation->set_rules('phone_number', 'Phone Number', 'trim|required');
    $this->form_validation->set_rules('email', 'Email', 'trim|required');



    if($this->form_validation->run() == FALSE)
    {
        $data['main_content'] = "league/update_contact";
        $this->load->view('includes/template', $data);
    }

    else
    {           

        if($query = $this->add_contact_model->updateContact())
        {
            $this->load->view('includes/top');
            $this->load->view('includes/header');
            $this->load->view('league/contact_updated');
            $this->load->view('includes/footer');

        }
        else
        {
            $this->load->view('includes/top');
            $this->load->view('includes/header');
            $this->load->view('league/about_added');
            $this->load->view('includes/footer');       
        }
    }
}

如果where语句不存在,它会更新,但显然它只是更新所有内容,所以只是不确定应该如何编写where语句。

提前感谢您的任何帮助:)

2 个答案:

答案 0 :(得分:2)

function updateContact( $id ) {
....
$this->db->where('id', $id );
控制器中的

确保在执行模型方法

之前id(segment(3))有效

答案 1 :(得分:0)

只是添加bensius评论,你真的不应该在模型中有$this->uri->segment(3)。这应属于您的控制器。