我構建上傳圖像並將其存儲到數據庫中,我已經可以將多個圖像上傳到文件夾,但是我無法插入所有上傳的圖像名稱,並且我不知道如何插入到數據庫中,首先我發現錯誤時,將我的代碼表示爲錯誤發生,其次我不知道查詢將它放在數據庫中,如果圖像計數不同,例如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\")");
}