我在循环数据和保存到数据库时遇到问题。此插入数据的结果只包含一个。 insert()与insert_batch()有什么区别?对不起,我的键盘上的CTRL + K不起作用。
我的观点:
<?php echo form_open('proses_tambah_produk')?>
<input type="file" id="gambar2" name="gambar_tambah[]" class="form-control" style="width:90%;display:initial;margin-right:10px;margin-bottom:5px;">
<label style="background-color:red;color:white;border-radius:50%;padding:3px;" id="idGambar2" class="hapus_gambar glyphicon glyphicon-remove"></label>
<input type="file" id="gambar2" name="gambar_tambah[]" class="form-control" style="width:90%;display:initial;margin-right:10px;margin-bottom:5px;">
<label style="background-color:red;color:white;border-radius:50%;padding:3px;" id="idGambar2" class="hapus_gambar glyphicon glyphicon-remove"></label>
<?php echo form_close()?>
我的控制员:
function proses_tambah_produk(){
$config['upload_path'] = 'assets/img/produk';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 1000;
$config['overwrite'] = TRUE;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
$files = $_FILES;
$count = count($_FILES['gambar_tambah']['name']);
for($i=0; $i<$count; $i++)
{
$_FILES['gambar_tambah']['name']= $files['gambar_tambah']['name'][$i];
$_FILES['gambar_tambah']['type']= $files['gambar_tambah']['type'][$i];
$_FILES['gambar_tambah']['tmp_name']= $files['gambar_tambah']['tmp_name'][$i];
$_FILES['gambar_tambah']['error']= $files['gambar_tambah']['error'][$i];
$_FILES['gambar_tambah']['size']= $files['gambar_tambah']['size'][$i];
$this->upload->do_upload('gambar_tambah');
$upload_data = $this->upload->data();
$name_array[] = $upload_data['file_name'];
$fileName = $upload_data['file_name'];
$images[] = $fileName;
}
$fileName = $images;
$tambahan = $_FILES['gambar_tambah']['name'];
$this->produk_adm->add($data, $gambar, $tambahan);
}
我的模特:
function add($tambahan){
$last_insert_id = $this->db->insert_id();
$data_gambar = array(
'id_produk' => $last_insert_id,
'gambar' => $tambahan,
);
$this->db->insert('produk_image', $data_gambar);
return $this->db->insert_id();
}
答案 0 :(得分:2)
$filesCount = count($_FILES['photo_gallery']['name']);
for($i = 0; $i < $filesCount; $i++){
$_FILES['gambar_tambah']['name'] = $_FILES['photo_gallery']['name'][$i];
$_FILES['gambar_tambah']['type'] = $_FILES['photo_gallery']['type'][$i];
$_FILES['gambar_tambah']['tmp_name'] = $_FILES['photo_gallery']['tmp_name'][$i];
$_FILES['gambar_tambah']['error'] = $_FILES['photo_gallery']['error'][$i];
$_FILES['gambar_tambah']['size'] = $_FILES['photo_gallery']['size'][$i];
$file_name=$this->crud->upload_file('gambar_tambah',$upload_image_path);
$image_data[$i]['image'] = $file_name;
$this->crud->add('table_name',$image_data[$i]);
}
答案 1 :(得分:0)
你不能两次使用相同的id名称。每个字段的id值必须是唯一的。您上传的代码错误,您正在分配值
$_FILES['gambar_tambah']['name']= $files['gambar_tambah']['name'][$i];
你不能这样做,你只需要为变量赋值
$image_name = $_FILES['gambar_tambah']['name'];
并在每次循环运行时将其插入到数据库中,或者通过implode函数将其插入到一个字段中。
答案 2 :(得分:0)
当您调用模型时,您使用了三个参数,而在模型中则使用了一个参数。
使用
$this->load->model('produk/adm');
$this->produk_adm->add($tambahan);
而不是
$this->produk_adm->add($data, $gambar, $tambahan);
和模型
function add($tambahan){
$last_insert_id = $this->db->insert_id();
$data_gambar = array(
'id_produk' => $last_insert_id, // if it is auto increment in db, remove this line
'gambar' => $tambahan,
);
$this->db->insert('produk_image', $data_gambar);
return $this->db->insert_id();
}