2016-02-22 111 views
-1

,如果我插入到數據庫中,我想在數據庫中的例子不同領域 image1_name image2_name 第一的形象在這裏在這裏第二圖像 我只知道如何插入到被插入到兩個像一個笨插入多個圖片到不同的數據庫字段

控制器

function do_upload() 
{ 
    $config['upload_path'] = './assets/'; 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = '2000'; 
    $config['max_width'] = '2000'; 
    $config['max_height'] = '2000'; 
    $config['new_image'] = './assets/'; 

    $config['overwrite'] = TRUE; 
    $this->load->library('upload', $config); 
    $this->form_validation->set_rules('name', 'Category Name', 'required'); 
    if (!$this->upload->do_upload() || !$this->form_validation->run()) { 
     $error = array('error' => $this->upload->display_errors()); 
     $this->session->set_flashdata("message2","Product not added"); 
     redirect('add_products','refresh'); 
    } else { 

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


     $file = array(
      'img_name' => $data['raw_name'], 
      'thumb_name' => $data['raw_name'] . '_thumb', 
      'ext' => $data['file_ext'], 
      'category' =>$this->input->post('name') 


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

     if($this->User->insert_cat($file) === TRUE) 
     { 
      $this->session->set_flashdata("message","You Have Successfully Added a new Category!"); 
      redirect('add_category','refresh'); 
     }else 
     { 
      $this->session->set_flashdata("message2","Category not added"); 
      redirect('add_category','refresh'); 
     } 
    } 
} 

視圖

<div class="panel panel-default"> 
    <div class="panel-body"> 
     <?php echo form_open_multipart('add_new_category/do_upload');?> 
     <center> 
     <?php if (validation_errors()): ?> 

      <div class="alert alert-danger alert-dismissible" role="alert" style="width: 700px;"> 
       <?php echo validation_errors(); ?> 
      </div> 
     <?php endif ?> 


     <div class="form-group"> 
      <label class="col-sm-2 control-label" style=" color: white"></label> 

      <label class="col-sm-2 control-label">Category Image</label> 
      <div class="col-sm-5"> 
       <input type="file" class="form-control" placeholder="" name="userfile"> 
      </div> 

     </div> 
     <br> <br> <br> 
     <div class="form-group"> 
      <label class="col-sm-2 control-label" style=" color: white"></label> 

      <label class="col-sm-2 control-label">Category Name</label> 
      <div class="col-sm-5"> 
       <input type="text" class="form-control" placeholder="ex. coffee" 
       name="name" 
       value="<?php echo set_value('name'); ?>"> 
      </div> 

     </div> 

模式

public function insert_cat($file) 
{ 
    // $this->db->insert('product_table',$file); 
    if($this->db->insert('product_category', $file)) 
    { 
     return TRUE; 
    }else 
    { 
     return FALSE; 
    } 
} 
+0

我可以看到你的觀點只有一個圖像輸入文件....你想上傳多個圖像相同的輸入文件..? – Yash

+0

[PHP上傳多張圖片並插入數據庫]可能重複(http://stackoverflow.com/questions/21772358/php-uploading-multiple-images-and-inserting-into-database) –

+0

還沒有嘗試過多次上傳它只有一個,我想上傳兩個圖像separetly到數據庫中的兩個不同的領域 – Christian

回答

0

再添加一個功能模型

public function insert_cat1($file){ 
      $this->db->insert('product_table',$file); 

      { 
       return TRUE; 
      }else 
      { 
       return FALSE; 
      } 
     } 

And change the controller 


    function do_upload() { 
      $config['upload_path'] = './assets/'; 
      $config['allowed_types'] = 'gif|jpg|png'; 
      $config['max_size'] = '2000'; 
      $config['max_width'] = '2000'; 
      $config['max_height'] = '2000'; 
      $config['new_image'] = './assets/'; 

      $config['overwrite'] = TRUE; 
      $this->load->library('upload', $config); 
      $this->form_validation->set_rules('name', 'Category Name', 'required'); 
      if (!$this->upload->do_upload() || !$this->form_validation->run()) { 
       $error = array('error' => $this->upload->display_errors()); 
       $this->session->set_flashdata("message2","Product not added"); 
      redirect('add_products','refresh'); 
      } else { 

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


       $file = array(
        'img_name' => $data['raw_name'], 
        'thumb_name' => $data['raw_name'] . '_thumb', 
        'ext' => $data['file_ext'], 
        'category' =>$this->input->post('name') 


       ); 
        $data = array('upload_data' => $this->upload->data()); 
       $insert1=$this->User->insert_cat($file); 
       $insert2=$this->User->insert_cat1($file); 
       if(($insert1)&&($insert2)==TRUE) 
     { 
      $this->session->set_flashdata("message","You Have Successfully Added a new Category!"); 
      redirect('add_category','refresh'); 
     }else 
     { 
      $this->session->set_flashdata("message2","Category not added"); 
      redirect('add_category','refresh'); 
      } 
      } 
      } 
+0

不工作,我只是將第一個圖像添加到這兩個字段 – Christian

+0

你插入$文件在這兩個表 – Angel

+0

他們是在同一張桌子上與不同的領域 – Christian