2017-06-16 145 views
1

我想上傳多個圖像,我一次插入數據到多個表。當用戶填寫表格時,首先將一些數據插入到table A中,並將inserted_id插入table B中。這是我的編碼看起來像。使用codeigniter插入多個圖像

控制器

$filesCount = count($_FILES['picture']['name']);  //my input file name = 'picture' 

for ($i = 0; $i < $filesCount; $i++) { 

    $_FILES['userFile']['name'] = $_FILES['picture']['name'][$i]; 
    $_FILES['userFile']['type'] = $_FILES['picture']['type'][$i]; 
    $_FILES['userFile']['tmp_name'] = $_FILES['picture']['tmp_name'][$i]; 
    $_FILES['userFile']['error'] = $_FILES['picture']['error'][$i]; 
    $_FILES['userFile']['size'] = $_FILES['picture']['size'][$i]; 

    $config['upload_path']   = '/uploads/user/test/'; 
    $config['allowed_types']  = 'gif|jpg|png'; 
    $config['max_size']    = 0; 

    $new_name = uniqueId(); 
    $config['file_name'] = $new_name; 

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

    if($this->upload->do_upload('userFile')){ 
     $fileData = $this->upload->data(); 
     $uploadData[$i]['file_name'] = $fileData['file_name']; 
    } 
} 


if(!empty($uploadData)) { 
    $this->Insert_model->setImage($uploadData); 

    $isCreate = $this->Insert_model->createImage($uploadData); 

} 

Insert_model

public function createImage($data = array()){ 
    $this->db->trans_begin(); 

    $userInfo = array(
     'user_id'   => $this->getUserId(),  //UserId fetch from session 
     'title'   => $this->getTitle(), 
     'description'  => $this->getDescription(), 
    ); 

    $this->db->insert('UserInfo', $userInfo); //Data inserted to table UserInfo first 

    $insert_id = $this->db->insert_id();  //And getting the inserted_id 

    $data[] = array(
     'user_id'   => $this->getUserId(), 
     'title_id'   => $insert_id,   //Insert Inserted id 
     'image_name'  => $this->getImage(), 
    ); 

    $this->db->insert_batch('UserImage', $data);  //Insert data to table UserImage 

    if ($this->db->trans_status() === FALSE) 
    { 
     $this->db->trans_rollback(); 
    } 
    else 
    { 
     $this->db->trans_commit(); 
     return ($this->db->affected_rows() != 1) ? false : true; 
    } 

} 

數據的輸出插入到表UserImage

Array 
(
    [0] => Array 
     (
      [file_name] => 5943442cd1380.jpg 
     ) 

    [1] => Array 
     (
      [file_name] => 5943442cd1380.png 
     ) 

    [2] => Array 
     (
      [user_id] => 2 
      [title_id] => 1 
      [image_name] => Array 
       (
        [0] => Array 
         (
          [file_name] => 5943442cd1380.jpg 
         ) 

        [1] => Array 
         (
          [file_name] => 5943442cd1380.png 
         ) 

       ) 

     ) 

) 

使用輸出,無法將數據插入到第二個表中。

預期產出將

Array 
(
    [0] => Array 
     (
      [user_id] => 2 
      [title_id] => 1 
      [file_name] => 5943442cd1380.jpg 
     ) 

    [1] => Array 
     (
      [user_id] => 2 
      [title_id] => 1 
      [file_name] => 5943442cd1380.png 
     ) 

) 
+0

這是因爲在模型中,你的參數是'$ data'。而且你只在控制器中插入文件名並傳遞給模型。 –

+0

我該如何從控制器獲取'inserted_id'? –

+0

你有沒有嘗試過在多個圖像,如果任何圖像不正確或不符合標準那麼會發生什麼? –

回答

2

你必須改變這一部分。

$data[] = array(
    'user_id'   => $this->getUserId(), 
    'title_id'   => $insert_id,   //Insert Inserted id 
    'image_name'  => $this->getImage(), 
); 


foreach($data as $row) { // here data is from parameter 
    $data1[] = array(
     'user_id'   => $this->getUserId(), 
     'title_id'   => $insert_id,   //Insert Inserted id 
     'image_name'  => $row['file_name'] // this line is changed 
    ); 
} 

$this->db->insert_batch('UserImage', $data1);// variable name is changed. 
+0

謝謝你的解決方案。我設法得到正確的輸出,但我得到錯誤'未定義的索引:file_name'在線''image_name'=> $ row ['file_name']' –

+0

哦,我只是在編碼時犯了一些錯誤。您的解決方案完美運作謝謝! –

+0

很高興我可以幫助你...乾杯.. –