【问题标题】:insert multiple image data into database with codeigniter使用codeigniter将多个图像数据插入数据库
【发布时间】: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


【解决方案1】:
$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]);
                }

【讨论】:

    【解决方案2】:

    您不能两次使用相同的 id 名称。每个字段的 id 值必须是唯一的。您的上传代码错误,您分配的值为 $_FILES['gambar_tambah']['name']= $files['gambar_tambah']['name'][$i]; 你不能这样做,你只需将值赋给一个变量,比如 $image_name = $_FILES['gambar_tambah']['name']; 并在每次循环运行时将其插入数据库或制作数组并将其插入内爆函数的一个字段中。

    【讨论】:

      【解决方案3】:

      当您调用模型时,您使用了三个参数,而在模型中您使用了一个。

      使用

       $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();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-13
        • 1970-01-01
        • 2021-01-19
        • 2013-04-04
        • 2016-09-18
        • 2017-02-11
        • 2017-07-13
        • 1970-01-01
        相关资源
        最近更新 更多