如何使用codeigniter更新记录?

时间:2018-07-11 13:02:34

标签: php codeigniter

我正在尝试更新记录,但是我不知道为什么我的记录没有得到更新。我正在尝试,但没有得到想要的结果。即使验证也不起作用。

控制器部分

public function updatedata()
{
    $id=$this->input->get('id');
    $result['data']=$this->Form_model->displayrecordsById($id);
    $this->load->view('update_records',$result);

    if($this->input->post('update'))
    {
        $this->form_validation->set_rules('fname', 'First name', 'required');
        $this->form_validation->set_rules('lname', 'Last name', 'required');
        $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[form.username]');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[form.email]');

        if ($this->form_validation->run() == TRUE)
        {
            $config['upload_path']   = './uploads/'; 
            $config['allowed_types'] = 'gif|jpg|png'; 
            $this->load->library('upload', $config);

            if($this->upload->do_upload('filename'))
            {
                $fn=$this->input->post('fname');
                $ln=$this->input->post('lname');
                $un=$this->input->post('username');
                $em=$this->input->post('email');
                $fi= $this->upload->data('file_name');
                $this->Form_model->updaterecords($fn,$ln,$un,$em,$fi,$id);
                echo 'Successfully updated your record';
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

尝试以下代码以帮助您查明错误。但是很可能您没有定义$ id ...

public function updatedata()
{

$id=$this->input->get('id');

if($this->input->post('update'))
{
    $this->form_validation->set_rules('fname', 'First name', 'required');
    $this->form_validation->set_rules('lname', 'Last name', 'required');
    $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[form.username]');
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[form.email]');

    if ($this->form_validation->run() == FALSE)
    {

        echo validation_errors();
        exit();

    } else {

        $config['upload_path']   = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $this->load->library('upload', $config);

        if($this->upload->do_upload('filename'))
        {

            $fn=$this->input->post('fname');
            $ln=$this->input->post('lname');
            $un=$this->input->post('username');
            $em=$this->input->post('email');
            $fi= $this->upload->data('file_name');

            // $id is not defined below.... you will need to define this I would imagine!

            $this->Form_model->updaterecords($fn,$ln,$un,$em,$fi,$id);
            echo 'Successfully updated your record';
            exit();

        } else {

            echo $this->upload->display_errors();
            exit();

        }

    }

}

$result['data']=$this->Form_model->displayrecordsById($id);
$this->load->view('update_records',$result);

}