php foreach 中的多个图像一起保存在 codeigniter 的不同行中

multiple images in php foreach getting saved all together in different rows in codeigniter

我有一个 codeigniter 网站,它有一个允许用户多次上传一些数据的表单,

我的观点如下:

 <input type="text" name="pname[]">
  <input type="file" name="pimage[]" multiple="multiple">
  
  <input type="text" name="pname[]" >
  <input type="file" name="pimage[]" multiple="multiple">

我的控制器是这样的:

 if(isset($_POST['addproduct']))
                                  {
foreach($this->input->post('pname') as $row =>$value)  {

  $this->load->library('upload');
  $image = array();
  $ImageCount = count($_FILES['pimage']['name']);
  for($i = 0; $i < $ImageCount; $i++){
  $_FILES['file']['name']       = $_FILES['pimage']['name'][$i];
  $_FILES['file']['type']       = $_FILES['pimage']['type'][$i];
  $_FILES['file']['tmp_name']   = $_FILES['pimage']['tmp_name'][$i];
  $_FILES['file']['error']      = $_FILES['pimage']['error'][$i];
  $_FILES['file']['size']       = $_FILES['pimage']['size'][$i];

  $uploadPath = './uploads/products/';
  $config['upload_path'] = $uploadPath;
  $config['allowed_types'] = 'jpg|jpeg|png|gif';

  $this->load->library('upload', $config);
  $this->upload->initialize($config);

  if($this->upload->do_upload('file')){
  $imageData = $this->upload->data();
   $uploadImgData[] = $imageData['file_name'];

  }
  }
$pname = $this->input->post('pname')[$row];
$pimage = $uploadImgData;
                                        
 $insertUserData = $this->category->addbulkproducts($pname,$pimage)
       }

这里的问题是图片,如果我为第一个产品上传 2 张图片,为第二个产品上传 2 张图片,所有 4 张图片都会上传 2 次并且所有这些图片都保存在产品行中,请参阅我的table低于

谁能告诉我这里出了什么问题,在此先感谢

您有两个重复的输入,只需删除一个:

<input type="text" name="pname[]">
<input type="file" name="pimage[]" multiple="multiple">
  

如果您想添加多个数据,只需更改它们的名称即可:

<input type="text" name="pname[]">
<input type="file" name="pimage0[]" multiple="multiple">

<input type="text" name="pname[]">
<input type="file" name="pimage1[]" multiple="multiple">

并记得在您的控制器中更新:

if (isset($_POST['addproduct'])) {
            foreach ($this->input->post('pname') as $row => $value) {
                $file_input_name = 'pimage' . $row;
                $uploadImgData = [];
                if (isset($_FILES[$file_input_name])) {

                    $ImageCount = count($_FILES[$file_input_name]['name']);
                    for ($i = 0; $i < $ImageCount; $i++) {
                        $_FILES['file']['name'] = $_FILES[$file_input_name]['name'][$i];
                        $_FILES['file']['type'] = $_FILES[$file_input_name]['type'][$i];
                        $_FILES['file']['tmp_name'] = $_FILES[$file_input_name]['tmp_name'][$i];
                        $_FILES['file']['error'] = $_FILES[$file_input_name]['error'][$i];
                        $_FILES['file']['size'] = $_FILES[$file_input_name]['size'][$i];
                    }
                    $uploadPath = './uploads/products/';
                    $config['upload_path'] = $uploadPath;
                    $config['allowed_types'] = 'jpg|jpeg|png|gif';
                    $this->load->library('upload', $config);
                    $this->upload->initialize($config);

                    if ($this->upload->do_upload('file')) {
                        $imageData = $this->upload->data();
                        $uploadImgData[] = $imageData['file_name'];
                    }
                }

                $pname = $this->input->post('pname')[$row];
                $pimage = $uploadImgData;

                $insertUserData = $this->category->addbulkproducts($pname, $pimage);
            }
        }

更新脚本以添加更多产品:

<script>
  var loop_count = 1;

  function add_more() {
    loop_count++;

    var html = '<div class="row" id="product_attr_' + loop_count + '">';



    html += '<div class="form-group col-md-6"><label for="inputEmail4">Product Name</label><input type="text" name="pname[]" class="form-control" id="inputEmail4"></div><div class="form-group col-md-6"><label for="inputEmail4">Product Price</label><input type="text" name="pprice[]" class="form-control" id="inputEmail4"></div><div class="form-group col-md-6"><label for="inputEmail4">Offer Price</label><input type="text" name="offerprice[]" class="form-control" id="inputEmail4"></div><div class="form-group col-md-6"><label for="inputEmail4">Brand Name</label><input type="text" name="brandname[]" class="form-control" id="inputEmail4"></div><div class="form-group col-md-6"><label for="inputEmail4">PIN Codes</label><textarea name="pincode[]" class="form-control" id="exampleFormControlTextarea1" placeholder="500028, 500056, etc" rows="3"></textarea></div><div class="form-group col-md-6"><label for="inputEmail4">XS</label><input type="text" name="xssize[]" placeholder="Stock" class="form-control" id="inputEmail4"></div><div class="form-group col-md-6"><label for="inputEmail4">S</label><input type="text" name="ssize[]" class="form-control" placeholder="Stock" id="inputEmail4"></div><div class="form-group col-md-6"><label for="inputEmail4">M</label><input type="text" name="msize[]" class="form-control" placeholder="Stock" id="inputEmail4"></div><div class="form-group col-md-6"><label for="inputEmail4">L</label><input type="text" name="lsize[]" class="form-control" placeholder="Stock" id="inputEmail4"></div><div class="form-group col-md-6"><label for="inputEmail4">XL</label><input type="text" name="xlsize[]" class="form-control" placeholder="Stock" id="inputEmail4"></div><div class="form-group col-md-6"><label for="inputEmail4">XXL</label><input type="text" name="xxlsize[]" class="form-control" placeholder="Stock" id="inputEmail4"></div><div class="form-group col-md-6"><label for="inputEmail4">3XL</label><input type="text" name="3xlsize[]" class="form-control" placeholder="Stock" id="inputEmail4"></div><div class="form-group col-md-6"><label for="inputEmail4">Description</label><input type="text" name="description[]" class="form-control" placeholder="Description" id="inputEmail4"></div><div class="form-group col-md-6"><label for="inputEmail4">Material</label><input type="text" name="material[]" class="form-control" placeholder="Material" id="inputEmail4"></div><div class="form-group col-md-6"><label for="inputEmail4">SKU</label><input type="text" name="sku[]" class="form-control" placeholder="SKU" id="inputEmail4"></div><div class="form-group col-md-6"><label for="inputEmail4">Product Image</label><input type="file" id="myfile" class="form-control" name="pimage' + (loop_count - 1) + '[]" multiple="multiple"></div><div class="form-group col-md-6"><label for="inputPassword4">Status</label><select class="form-control" name="status[]" aria-label="Default select example"><option value="Active">Active</option><option value="Inactive">Inactive</option></select></div>';

    html += '<div class="col-md-2"> <label <button type="button" class=" btn btn-danger " onclick=remove_more("' + loop_count + '")>REMOVE </button></div>';


    jQuery('#product_attr_box').append(html)
  }

  function remove_more(loop_count) {
    jQuery('#product_attr_' + loop_count).remove();
  }
</script>