怎么做验证对不对?

时间:2019-03-28 04:27:49

标签: php mysql codeigniter

验证时我将图像上传到数据库,图像没有进入数据库,如果不是我的说法是不是?

public function simpan(){
    $this->load->library('form_validation'); // Load library form_validation untuk proses validasinya
    $this->form_validation->set_rules('tgl_lahir', 'Tanggal Lahir', 'required');
    $this->form_validation->set_rules('id', 'id', 'required|is_unique[biodata.id]');
    $this->form_validation->set_message('required', '%s <font color="red">silahkan di isi</font>');
    $this->form_validation->set_message('is_unique', '{field} ini sudah dipakai, silahkan mengganti dengan yang lain');

    if (!empty($_FILES['photo']['name'])) {
        $upload = $this->_do_upload('photo');
        $data['photo'] = $upload;
    }elseif ($this->form_validation->run() == FALSE){
        $this->load->view('users/formusers');
    }else{
        $data = array(
            "tgl_lahir"       => $this->input->post('tgl_lahir'),
            "id"              => $this->input->post('id')
        );
    }

    $this->M_formulir->simpan($data);
     redirect('users/formusers/tampil','refresh',$data);
}

函数上传,请更正我的代码

private function _do_upload()
{
    $config['upload_path'] = './assets/back/images/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
     $config['width']= 600;
    $config['height']= 400;
    $config['encrypt_name'] = TRUE;
    $this->upload->initialize($config); 

    $this->load->library('upload', $config);
    if (!$this->upload->do_upload('photo')) {
        $this->session->set_flashdata('msg', $this->upload->display_errors('',''));
        redirect('dashboard','refresh');
    }
    return $this->upload->data('file_name');
}

1 个答案:

答案 0 :(得分:0)

尝试一下

已更新

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

   //When validation will return TRUE then insert data in model
   if (!empty($_FILES['photo']['name'])) {
       $upload = $this->_do_upload('photo');
       $data['photo'] = $upload['file_name'];
    }else{
       $data = array(
           "tgl_lahir"       => $this->input->post('tgl_lahir'),
           "id"              => $this->input->post('id')
       );
    }
    $this->M_formulir->simpan($data);
    //Data inserted successfully. Redirect according to requirements

}else{
   return $this->load->view('users/formusers');
}



private function _do_upload()
{
    $config['upload_path'] = './assets/back/images/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
     $config['width']= 600;
    $config['height']= 400;
    $config['encrypt_name'] = TRUE;
    $this->upload->initialize($config); 

    $this->load->library('upload', $config);
    if (!$this->upload->do_upload('photo')) {
        $this->session->set_flashdata('msg', $this->upload->display_errors('',''));
        redirect('dashboard','refresh');
    }else{
        return $this->upload->data();
    }

}