2015-05-30 97 views
0

我構建上傳圖像並將其存儲到數據庫中,我已經可以將多個圖像上傳到文件夾,但是我無法插入所有上傳的圖像名稱,並且我不知道如何插入到數據庫中,首先我發現錯誤時,將我的代碼表示爲錯誤發生,其次我不知道查詢將它放在數據庫中,如果圖像計數不同,例如1-10圖像,最後一個問題,如果我查詢「 SELECT id ...「,我想返回它,是否有方法將其返回到字符串或int?如果我使用row(),它將返回stdClass對象。請幫幫我, 下面是我的代碼:codeigniter插入多個圖像名稱到數據庫中

控制器:

$this->load->library("myupload", "form_validation"); 
     $this->load->model("testModel"); 
     $barangImage = array(); 

     if($this->input->post("formSubmit")) { 
      $this->form_validation->set_rules("nama", "Nama", "required|trim"); 

      if($this->form_validation->run()) { 
       $insertData = array(
        "nama" => $this->input->post("nama") 
       ); 
       if($id = $this->testModel->add($insertData)) { 

        //print_r($id); 

        if(isset($_FILES) && $image = $this->myupload->uploadFile($_FILES)) { 
         //$image here is already fill with all images name 

         if(isset($image["error"]) && $image["error"]) { 
          echo $image["error"]; 
         }else { 
          foreach($image as $img) { 
           $barangImage = array(
             "gambar" => $img, 
             "barangid" => $id 
           ); 

          } 
          //but when i put into barangImage, 
          //it only stored last image name 
          print_r($barangImage); 
          //output `Array ([gambar] => 2.JPG [barangid] => Array ([id] => 52))` 
         } 
        } 

        if($id = $this->testModel->add_images($barangImage)) { 
         echo "SUCCESS !!!"; 
        }else { 
         echo "FAIL INSERT IMAGES!!!"; 
        } 
       }else { 
        echo "FAIL INSERT DATA NAMA"; 
       } 
      }else { 
       echo "FAIL VALIDASI RUN"; 
      } 
     } 

型號:

public function add($newData){ 
     $this->db->insert("cobabarang", $newData); 

     $nama = $newData["nama"]; 
     $id = $this->db->query("SELECT id FROM cobabarang WHERE nama = \"$nama\""); 

     return $id->row_array(); 
    } 

    public function add_images($newImage) { 

     //$this->db->insert("cobagambar", $newImage); 

     $id = $newImage["barangid"]["id"]; 
     $gambar = $newImage["gambar"]; 

     $this->db->query("INSERT INTO cobagambar(barangid, gambar1) VALUES($id, \"$gambar\")"); 
    } 

回答

0

這裏有個錯誤:

foreach($image as $img) 
{ 
    $barangImage = array(
     "gambar" => $img, 
     "barangid" => $id 
    );  
} 

變化$barangImage$barangImage[]

當你把圖像放入數據庫時​​,我建議使用json_encode($barangImage),然後json_decode($images-json-string)當你要使用的圖像。

0

也有一些是不對您foreach循環

foreach($image as $img) { 
     $barangImage = array(
     "gambar" => $img //might be img['img'] I guess $img is again an array...you hvae to check that 
     "barangid" => $id //might be $img['id']check on this too..will be $img['id'] I guess 
    ); 

    } 

我的猜測是,$img又是一些關鍵的數組。你真的需要檢查這一點,你可以直接調用插入功能在foreach循環本身喜歡這個,

foreach($image as $img) { 
     $barangImage = array(
     "gambar1" => $img['img'], //I guess $img is again an array...you hvae to check that 
     "barangid" => $img['id'] //check on this too..will be $img['id'] I guess 
    ); 
      $id = $this->testModel->add_images($barangImage)); 
    } 

注:你的陣列barangImage中的keys必須在表的列名。即

gambar1 and barangid will be your column names. so you can directly use codeIgniter's active records. 

只要改變你的add_images功能

public function add_images($newImage) { 

    $this->db->insert("cobagambar", $newImage); 

} 
相關問題