2017-06-16 81 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

很高兴我可以帮助你...干杯.. –