2017-02-24 136 views
0

我有一個循環數據並保存到數據庫的問題。這個插入數據的結果只包含一個。 insert()和insert_batch()有什麼區別?我很抱歉我的鍵盤上的CTRL + K不起作用。插入多個圖像數據到codeigniter數據庫

我的觀點:

<?php echo form_open('proses_tambah_produk')?> 
<input type="file" id="gambar2" name="gambar_tambah[]" class="form-control" style="width:90%;display:initial;margin-right:10px;margin-bottom:5px;"> 
<label style="background-color:red;color:white;border-radius:50%;padding:3px;" id="idGambar2" class="hapus_gambar glyphicon glyphicon-remove"></label> 
<input type="file" id="gambar2" name="gambar_tambah[]" class="form-control" style="width:90%;display:initial;margin-right:10px;margin-bottom:5px;"> 
<label style="background-color:red;color:white;border-radius:50%;padding:3px;" id="idGambar2" class="hapus_gambar glyphicon glyphicon-remove"></label> 
<?php echo form_close()?> 

我的控制器:

function proses_tambah_produk(){ 
     $config['upload_path']   = 'assets/img/produk'; 
     $config['allowed_types']  = 'gif|jpg|png|jpeg'; 
     $config['max_size']    = 1000; 
     $config['overwrite']    = TRUE; 
     //$config['max_width']   = 1024; 
     //$config['max_height']   = 768; 
     $this->load->library('upload', $config); 

     $files = $_FILES; 
     $count = count($_FILES['gambar_tambah']['name']); 
     for($i=0; $i<$count; $i++) 
       { 
       $_FILES['gambar_tambah']['name']= $files['gambar_tambah']['name'][$i]; 
       $_FILES['gambar_tambah']['type']= $files['gambar_tambah']['type'][$i]; 
       $_FILES['gambar_tambah']['tmp_name']= $files['gambar_tambah']['tmp_name'][$i]; 
       $_FILES['gambar_tambah']['error']= $files['gambar_tambah']['error'][$i]; 
       $_FILES['gambar_tambah']['size']= $files['gambar_tambah']['size'][$i]; 
       $this->upload->do_upload('gambar_tambah'); 
       $upload_data = $this->upload->data(); 
       $name_array[] = $upload_data['file_name']; 
       $fileName = $upload_data['file_name']; 
       $images[] = $fileName; 

       } 
       $fileName = $images; 

      $tambahan = $_FILES['gambar_tambah']['name']; 

      $this->produk_adm->add($data, $gambar, $tambahan); 

    } 

我的模型:

function add($tambahan){ 
     $last_insert_id = $this->db->insert_id(); 

     $data_gambar = array(
      'id_produk' => $last_insert_id, 
      'gambar' => $tambahan, 
     ); 
     $this->db->insert('produk_image', $data_gambar); 
     return $this->db->insert_id(); 
    } 
+0

您只能使用客戶端JavaScript/HTML/CSS,現場代碼段功能,應根據每個面板上的相應標籤顯而易見。 – Sparky

+0

當您調用模型時,您已使用$ this-> produk_adm-> add($ data,$ gambar,$ tambahan); –

回答

0

你不能用相同的ID名稱兩次。每個字段的id值必須是唯一的。您上傳的代碼是錯誤的,你要分配的 $_FILES['gambar_tambah']['name']= $files['gambar_tambah']['name'][$i]; 值你不能做到這一點insted的你只是將值分配給一個變量像 $image_name = $_FILES['gambar_tambah']['name']; 並插入到數據庫中,每次當循環運行或做它陣列並將其插入整合到一個由implode函數提交的文件中。

2
$filesCount = count($_FILES['photo_gallery']['name']);  
for($i = 0; $i < $filesCount; $i++){ 
        $_FILES['gambar_tambah']['name'] = $_FILES['photo_gallery']['name'][$i]; 
        $_FILES['gambar_tambah']['type'] = $_FILES['photo_gallery']['type'][$i]; 
        $_FILES['gambar_tambah']['tmp_name'] = $_FILES['photo_gallery']['tmp_name'][$i]; 
        $_FILES['gambar_tambah']['error'] = $_FILES['photo_gallery']['error'][$i]; 
        $_FILES['gambar_tambah']['size'] = $_FILES['photo_gallery']['size'][$i]; 
        $file_name=$this->crud->upload_file('gambar_tambah',$upload_image_path); 
        $image_data[$i]['image'] = $file_name; 
        $this->crud->add('table_name',$image_data[$i]); 
       } 
0

當您調用模型時,您已經使用了三個參數,並且在模型中使用了一個。

使用

$this->load->model('produk/adm'); 
$this->produk_adm->add($tambahan); 

,而不是

$this->produk_adm->add($data, $gambar, $tambahan); 

,並在模型

function add($tambahan){ 
    $last_insert_id = $this->db->insert_id(); 

    $data_gambar = array(
     'id_produk' => $last_insert_id, // if it is auto increment in db, remove this line 
     'gambar' => $tambahan, 
    ); 
    $this->db->insert('produk_image', $data_gambar); 
    return $this->db->insert_id(); 
}