开发者

CodeIgniter. Update where id problem

I'm trying to update a contact and only update the contact depending on the id.

Model:

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


}   

Controller:

    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('in开发者_如何转开发cludes/top');
            $this->load->view('includes/header');
            $this->load->view('league/about_added');
            $this->load->view('includes/footer');       
        }
    }
}

It's updating if the where statement isn't there, but obviously it just updates everything, so just unsure of how the where statement should be written.

Thanks in advance for any help :)


function updateContact( $id ) {
....
$this->db->where('id', $id );

in your controller make sure that id (segment(3)) is valid before execute Model method


Just to add to bensius comment, you really shouldn't have $this->uri->segment(3) in the model. This should belong in your controller.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜