2013-03-28 43 views
0

我想用codeigniter中的描述上傳多個圖像。一切都很好,圖像上傳,圖像文件名發送到數據庫,但「描述」不會去數據庫,我錯在哪裏?用codeigniter在mysql中插入多行

form code is like this 
    File 1: <input type="file" name="image1" /><br /> 
    Descr: <input type="text" name="description[]" size="50" value="" /><br/><br/> 

    File 2: <input type="file" name="image2" /><br /> 
    Descr: <input type="text" name="description[]" size="50" value="" /><br/><br/> 

    File 3:<input type="file" name="image3" /><br /> 
    Descr: <input type="text" name="description[]" size="50" value="" /><br/><br/> 

和模式是這個

for($i = 1; $i < 6; $i++) { 
        /* Handle the file upload */ 
        $upload = $this->upload->do_upload('image'.$i); 
        /* File failed to upload - continue */ 
        if($upload === FALSE) continue; 
        /* Get the data about the file */ 
        $data = $this->upload->data(); 

        $uploadedFiles[$i] = $data; 
        /* If the file is an image - create a thumbnail */ 
        if($data['is_image'] == 1) { 
         $configThumb['source_image'] = $data['full_path']; 
         $this->image_lib->initialize($configThumb); 
         $this->image_lib->resize(); 
        } 


         $image['raw'] = $data['raw_name']; 
         $image['ext'] = $data['file_ext']; 
         $image['newsid'] = $_POST['newsid']; 
         $image['description'] = $_POST['description'][$i]; 
         $this->db->insert('multipics', $image); 
       } 

請注意,上面的代碼使多行插入,我只是需要說明字段數據庫

+0

您是否使用print_r($ _ POST)?是否發佈說明。 – Neo 2013-03-28 09:07:23

+0

是描述發佈[描述] =>數組 ( [0] => GGGG [1] => hhhhhhhhhhhhhh [2] => BBB ) – ktm 2013-03-28 09:14:40

+0

使用$這 - > DB-> last_query(),在$ this-> db-> insert語句之後,正確地生成插入語句。 – Neo 2013-03-28 09:16:37

回答

0

作爲解決辦法,我會首先在環路外獲得POST變量:

// (1) Get the POST description variable outside of the look  
// As a precaution, dump or echo $description to make sure it looks right... 

$description_ = $this->input->post('description'); 


for ($i = 1; $i < 6; $i++) 
{ 
    ... 
    // Put some checks to make sure $i is an index... 
    $image['description'] = $description[$i]; 
    ... 
} 

試試這個,讓我們知道你是怎麼做的!

0

更改更改名稱=「說明去]「to name =」description

+0

name =「description []」to name =「description does not work – ktm 2013-03-28 09:29:38

0

您可能還想看看CI的insert_batch()方法。這將允許您用一個查詢插入所有記錄。