【发布时间】:2017-02-25 18:31:13
【问题描述】:
我在循环数据和保存到数据库时遇到问题。这个插入数据的结果只包含一个。 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();
}
【问题讨论】:
-
您只能对客户端 JavaScript/HTML/CSS 使用实时代码片段功能,根据每个面板上的相应标签,这应该是显而易见的。
-
当你调用模型时你已经使用了 $this->produk_adm->add($data, $gambar, $tambahan);
标签: php codeigniter