I have the following controller code for editing a supplier form:
public function edit($supplier_id = NULL) {
if ($this->session->userdata ( 'username' ) === NULL) {
redirect ( 'login' );
} else {
// set validation rules
$this->form_validation->set_rules ( 'suppliers', 'Department', 'callback_combo_check' );
$this->form_validation->set_rules ( 'nome', 'Employee No', 'trim|required' );
$this->form_validation->set_rules ( 'morada', 'Employee Name', 'trim|required' );
$this->form_validation->set_rules ( 'cp', 'Employee Name', 'trim|required' );
$this->form_validation->set_rules ( 'localidade', 'Designation', 'trim|required' );
if ($this->form_validation->run () == FALSE) {
$data['supplier_data'] = $this -> Suppliers_model -> get_supplier($supplier_id);
if (empty($data['supplier_data'])) {
show_404();
}
$this->lang->load ( 'suppliers' );
$data['suppliers'] = $this->Suppliers_model->get_suppliers();
$data ['title'] = 'Dashboard';
$this->load->view ( 'templates/head', $data );
$this->load->view ( 'templates/menu' );
$this->load->view ( 'suppliers/edit', $data );
$this->load->view ( 'templates/scripts' );
$this->load->view ( 'templates/footer' );
} else {
// pass validation
$up_data = array (
'Tipo_Fornecedor' => $this->input->post ( 'suppliers' ),
'Nome_Fornecedor' => $this->input->post ( 'nome' ),
'Morada_Fornecedor' => $this->input->post ( 'morada' ),
'Codigo_Postal' => $this->input->post ( 'cp' ),
'Localidade' => $this->input->post ( 'localidade' )
);
// insert the form data into database
$this->db->where('Codigo_Fornecedor', $supplier_id);
$this->db->update( 'tabela_fornecedores', $up_data );
// display success message
$this->session->set_flashdata ( 'msg', '<div class="alert alert-success text-center">Supplier details updated to Database!!!</div>' );
redirect ('suppliers/edit/'.$supplier_id);
}
}
}
The result should be after updating the database, returning to the edit form with the OK message as is inside controller. Problem is that when i redirect at the end to the url, in this case it redirects to itself, the redirect loses the $suppliers_id parameter so the controller hits the 404 error, beucase it cannot find a id for the supplier.
Example: www.site.com/suppliers/edit/1
1 is the suppliers id, it loads the page correctly, i change whatever i need, submit the form, the values ARE updated, but when it hits the redirect part
redirect ('suppliers/edit/'.$supplier_id);
it goes to www.site.com/suppliers/edit/, since there is no id it hits the 404_show function on the start of the page.
What am i doing wrong, why does the redirect with $suppliers_id fail?
Best regards
Nuno Reis
Routing rule for the url
$route['suppliers/edit/(:any)'] = 'suppliers/edit/$1';