drz73366 2015-10-08 16:07
浏览 22
已采纳

CodeIgniter - 无法将id参数添加到redirect()

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';
  • 写回答

1条回答 默认 最新

  • 「已注销」 2015-10-09 23:20
    关注

    First thx for your input.

    The issue was:

    When opening the page to edit i got the id from the uri segment /$id (/3). When posting the else {} part of the statment as enforced, so no $id on the link, gave the 404 error.

    I just added a hidden field to the form that stores the /$id when edir form is loaded. When posted, reaches the else {} gets the $id from the post and then uses it for the query and the redirect.

    All is working fine now.

    Once more thx for the replies.

    Best regards

    Nuno Reis

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀