2017-07-04 54 views
0

我正在CodeIgniter框架上建立一個網站,以便用戶可以將網站上的產品上傳到數據庫。我試圖做一個功能,以便用戶可以上傳圖片到我的數據庫,但它並不真正工作。圖片數據庫上傳不起作用Codeigniter

這裏的一些信息: 數據庫表名:產品 我想要的圖片存儲在數據庫表的列名:product_foto 圖片文件夾:上傳

My controller: 
    <?php 
    defined('BASEPATH') OR exit('No direct script access allowed'); 
    class Product extends CI_Controller { 

    var $data = array(); 

    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('product_model'); 
     $this->load->helper(array('form', 'url')); 
    } 

    public function product_form() 
    { 
     $save = array(
      'product_naam'   => $this->input->post('product_naam'), 
      'product_beschrijving'   => $this->input->post('product_beschrijving'), 
      'product_categorie' => $this->input->post('product_categorie'), 
      'ophaal_plaats' => $this->input->post('ophaal_plaats'), 
      'product_foto' => $this->input->post('product_foto'), 
      'date_created' => date('Y-m-d'), 
      'date_updated' => date('Y-m-d') 
      ); 

     $this->product_model->saveProduct($save); 
     redirect('https://kadokado-ferran10.c9users.io/AlleCadeausController'); 
    } 


    public function upload(){ 
     $config['upload_path'] = './upload/'; 
     $config['allowed_types'] = 'jpg|jpeg|png'; 

     $this->load->library('upload', $config); 
     if(!$this->upload->do_upload('file')){ 
     $this->db->insert('products', array(
      'product_foto' => $this->upload->file_name 
      )); 
     $error = array('error'=>$this->upload->display_errors()); 
     $this->load->view('product_form', $error); 
     }else{ 
     $file_data = $this->upload->data(); 
     $data['img'] = base_url().'/upload/'.$file_data['file_name']; 
     header('location:https://kadokado-ferran10.c9users.io/Product/'); 
     } 
    } 

    } 

我的模型文件:

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 
class Product extends CI_Controller { 

    var $data = array(); 

我的視圖文件:

<?php echo form_open_multipart('Product/upload'); ?> 
    <input type="file" name="userfile" /> 

    <div class="form-group"> 
     <label for="name">Name</label> 
     <input type="text" name="name"> 
    </div> 

    <input type="submit" name="submit" value="test" /> 

</form> 

當我提交的圖片只被添加到上傳文件夾,但它不會加入到數據庫列的形式..

+0

認沽插入查詢條件。 –

回答

2

更改upload()功能跟隨其他

public function upload(){ 
    $config['upload_path'] = './upload/'; 
    $config['allowed_types'] = 'jpg|jpeg|png'; 

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

    if(!$this->upload->do_upload('userfile')){ 
     $error = array('error'=>$this->upload->display_errors()); 
     $this->load->view('product_form', $error); 
    }else{ 
     $file_data = $this->upload->data(); 
     $this->db->insert('products', array(
      'product_foto' => $file_data['file_name'] 
      )); 
     $data['img'] = base_url().'/upload/'.$file_data['file_name']; 
     header('location:https://kadokado-ferran10.c9users.io/Product/'); 
    } 
} 
+0

嘿我試過你的代碼和一個空的產品被添加到數據庫沒有名稱在圖片列 – lablanco

+0

你通過ajax上傳 –

+0

不,我沒有使用ajax – lablanco