CodeIgniter. Обновите, где проблема с id - PullRequest
1 голос
/ 21 марта 2011

Я пытаюсь обновить контакт и только обновить контакт в зависимости от идентификатора.

Модель:

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 ]

2 голосов
/ 21 марта 2011
function updateContact( $id ) {
....
$this->db->where('id', $id );

в вашем контроллере убедитесь, что id (сегмент (3)) действителен перед выполнением метода Model

0 голосов
/ 21 марта 2011

Просто чтобы добавить к комментарию bensius, у вас действительно не должно быть $this->uri->segment(3) в модели. Это должно принадлежать вашему контроллеру.

...