2015-07-04 79 views
0

我是新来的,很高兴认识你。codeigniter成功插入数据库但未能插入上传的文件

我在codeigniter中遇到了问题。

也许很难说,所以我会告诉我的所有代码:

我的控制器:

function upload_barang(){ 

    $this->admin_model->login(); 

    $this->admin_model->valid_product(); 

    $config['upload_path'] = './produk/'; 
    $config['allowed_types'] = 'jpg'; 
    $config['max_size'] = '100'; 
    $config['max_width'] = '400'; 
    $config['max_height'] = '400'; 

    $this->load->library('upload',$config); 

    $data['query'] = $this->db->get('kategori'); 

    if($this->form_validation->run() == FALSE && ! $this->upload->do_upload()) { 

    $data['error'] = $this->upload->display_errors(); 

    $data['success'] = ''; 

    $this->load->view('backoffice/tambahbarang',$data); 

    } else { 

     $upload_data = $this->upload->data(); 

     $data['error'] = ''; 

     $data = array(
     'idkategori' => $this->input->post('idkategori'), 
     'namabarang' => $this->input->post('nbarang'), 
     'image' => $upload_data['file_name'], 
     'status' => 1 
    ); 

    $data_insert = $this->db->insert('barang',$data); 

    if($data_insert == TRUE) { 

     $data['query'] = $this->db->get('kategori'); 

     $query = $this->db->get('barang'); 

     $rows = $query->row(); 

     $data['idbarang'] = $rows->idbarang; 

     $harga = array(
     'idbarang' => $data['idbarang'], 
     'satuan' => $this->input->post('stcbarang'), 
     'harga' => $this->input->post('hbarang') 
    ); 

    $this->db->insert('hargabarang',$harga); 

    $data['success'] = '<b>Barang Telah Ditambahkan</b>'; 

    $this->load->view('backoffice/tambahbarang',$data); 

    } else { 

    $data['success'] = '<b>Barang Gagal Ditambahkan</b>'; 

    $this->load->view('backoffice/tambahbarang',$data); 

    } 

    } 
} 

此代码成功插入所有的数据到数据库中,除了在列“形象”,我填'$ upload_data ['file_name']'在我的数据库中为空。

我的代码有什么问题?

请帮助谢谢

+0

为什么你写的查询在'controller'这是不好用MVC' – Saty

+0

的'在代码中删除此行'$尝试data ['error'] ='';' – Saty

+0

那只是为了测试,成功后我会改变它。 – gong

回答

0

您可以使用此格式:

/* 
    * ------- Start Image Upload--------- 
    */ 
    $config['upload_path'] = 'image/product_images/'; 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = '100'; 
    $config['max_width'] = '400'; 
    $config['max_height'] = '400'; 

    $this->load->library('upload', $config); 
    $this->upload->initialize($config); 
    $error=''; 
    $fdata=array(); 
    if (! $this->upload->do_upload('product_image')) 
    { 
      $error = $this->upload->display_errors(); 
      echo $error; 
      exit(); 
    } 
    else 
    { 
      $fdata = $this->upload->data(); 
      $data['product_image']=$config['upload_path'] .$fdata['file_name']; 
    } 

    /* 
    * --------End Image Upload--------- 
    */ 
    $this->super_admin_model->save_product_info($data); 
+0

嗯谢谢你的回答,但如何form_validation?如果我使用此代码仅用于上传图像,然后将它们保存到数据库中,那么表单验证如何?如何使用表单验证来组合它们? – gong

相关问题