dongshi9407 2019-03-28 04:27
浏览 49
已采纳

怎么做验证对吗?

I will upload an image to the database when doing validation, the image does not enter the database, is the statement if else I am wrong?

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);
}

function upload please correct my code

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条回答 默认 最新

  • dsv38843 2019-03-28 04:36
    关注

    Try this

    Updated

    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();
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有没有可以帮我搞一个微信建群链接,包括群名称和群资料群头像那种,不会让你白忙
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题