Php codeigniter将许多图像名称插入数据库
我正在构建上传图像并将其存储到数据库中,我已经可以将许多图像上传到文件夹中,但我无法插入所有上传的图像名称,而且我不知道如何插入到数据库中,首先,当出现错误时,我在下面的代码中添加了注释,第二,如果图像计数不同,我不知道要将其放入数据库中的查询,例如1-10个图像,最后一个问题,如果我确实查询“SELECT id…”并希望返回它,是否有方法将其返回到string或int中?如果我使用row(),它将返回stdClass对象。请帮帮我, 下面是我的代码: 控制器:Php codeigniter将许多图像名称插入数据库,php,mysql,image,codeigniter,insert,Php,Mysql,Image,Codeigniter,Insert,我正在构建上传图像并将其存储到数据库中,我已经可以将许多图像上传到文件夹中,但我无法插入所有上传的图像名称,而且我不知道如何插入到数据库中,首先,当出现错误时,我在下面的代码中添加了注释,第二,如果图像计数不同,我不知道要将其放入数据库中的查询,例如1-10个图像,最后一个问题,如果我确实查询“SELECT id…”并希望返回它,是否有方法将其返回到string或int中?如果我使用row(),它将返回stdClass对象。请帮帮我, 下面是我的代码: 控制器: $this->load-&
$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\")");
}
这里有一个错误:
foreach($image as $img)
{
$barangImage = array(
"gambar" => $img,
"barangid" => $id
);
}
将$barangImage
更改为$barangImage[]
当您将图像放入数据库时,我建议使用
json\u编码($barangImage)
,然后在使用图像时使用json\u解码($images-json-string)
。您的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
循环中调用insert函数,如下所示
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
中的键必须是表中的列名。i、 e
gambar1 and barangid will be your column names. so you can directly use codeIgniter's active records.
只需更改添加图像
功能即可
public function add_images($newImage) {
$this->db->insert("cobagambar", $newImage);
}