-2

I have a problem. here is my php & HTML:

<?php
      if ($_POST['btn_tambah'] == 'tambah') {

      $pathGambar = "gallery/" . $_FILES['gambar']['name'];         
      $SQL = "SELECT AUTO_INCREMENT as IDLapangan FROM information_schema.tables WHERE TABLE_SCHEMA = 'ta' AND TABLE_NAME = 'lapangan';";
      $res = mysql_query($SQL, $link);
      $row = mysql_fetch_object($res);
      foreach ($pathGambar as $path => $value) {
          $tambahGambar = mysql_query("INSERT INTO foto(path,id_portfolio) VALUES('" . $value . "',$row->IDPortfolio);");
      }
?>

<HTML><BODY>
  <div class="row">
   <div class="form-group">
      <div class="col-lg-3"><label>Gambar :</label></div>
      <div class="col-lg-2">
         <input type="file" name="gambar[]" class="form-control"  required>
      </div>
      <div class="col-lg-1">
       <a onclick="tambahGambarBaru()" class ="btn btn-info"> <i class="fa fa-plus"></i></a>
      </div>
    </div>
   </div>
  <div id="sembunyiGambar">
  </div>
</BODY></HTML>

here is my script:

 function tambahGambarBaru() {
            $('#sembunyiGambar').append(                
                '<div class="row" id="imgBaru">'
                + '<div class="form-group">'
                + '<div class="col-lg-3">'
                + '</div>'
                + '<div class="col-lg-4">'
                + ' <input id="filePic" accept="image/*" type="file" name="gambar[]" class="form-control"  required>'
                + '</div>'
                + '<div class="col-lg-1">'
                + '<a class ="btn btn-warning" onclick="hapus()"> <i class="fa fa-trash"></i></a>'
                + '</div>'
                + '</div>'
                + '</div>'
        )
        }
 function hapus() {
            $('#imgBaru').remove();
        }

here is my pic:

enter image description here

So the scenario is, when i click the "plus" button, it will show up the second textfile input. I want to insert them into database. but when i try to insert, it shows error

invalid argument supplied for foreach()

To show up the second textfile input, i use .append in javascript.

Qirel
  • 25,449
  • 7
  • 45
  • 62
daniel
  • 47
  • 1
  • 10
  • Possible duplicate of [Invalid argument supplied for foreach()](http://stackoverflow.com/questions/2630013/invalid-argument-supplied-for-foreach) – Qirel Jun 26 '16 at 14:32

1 Answers1

0

I'm sorry. it was my mistake. so, it should be like this:

$pathGambar = $_FILES['gambar']['name'];  
 foreach ($pathGambar as $path => $value) {
      $tambahGambar = mysql_query("INSERT INTO foto(path,id_portfolio) VALUES('" . $value . "',$row->IDPortfolio);");
  }

many thanks :)

daniel
  • 47
  • 1
  • 10