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 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题