title: uploading (multiple)image with data in same form
body: <p>i'm trying to make this form with pdo where you can insert a a data and multiple image in the same form and currently, this is my problem:
  html:</p>
  
  <pre><code>&lt;form enctype="multipart/form-data" name="postHome" action="&lt;?php echo htmlentities($_SERVER['PHP_SELF'])?&gt;" method="post"&gt;
  
              &lt;table border="0" style="width: auto; line-height: 300%"&gt;
                  &lt;p&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;u&gt;Advertisement Details:&lt;/u&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Title:&lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" name="home_adTitle" width="500px" placeholder="Title" class="text-input" required="required"&gt;&lt;/td&gt;
                 &lt;/tr&gt;
                 &lt;tr&gt;
                      &lt;td valign="top"&gt;Description:&lt;/td&gt;
                      &lt;td&gt;&lt;textarea rows="5" cols="50" input type="text" name="home_description" width="400px"&gt;&lt;/textarea&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                 &lt;/p&gt;
  
                 &lt;p&gt;
                 &lt;br&gt;
                 &lt;tr&gt;
                     &lt;td&gt;&lt;u&gt;Homestay Details:&lt;/u&gt;&lt;/td&gt;
                 &lt;/tr&gt;
                 &lt;tr&gt;
                     &lt;td&gt;Homestay Name:&lt;/td&gt;
                     &lt;td&gt;&lt;input type="text" name="home_name" width="500px" placeholder="Homestay name" class="text-input"&gt;&lt;/td&gt;
                 &lt;/tr&gt;
                 &lt;tr&gt;
                      &lt;td&gt;Category: &lt;/td&gt;
                      &lt;td&gt;&lt;select name="home_category"&gt;
                          &lt;option value&gt;Choose category&lt;/option&gt;
                          &lt;option value="1"&gt;Room&lt;/option&gt;
                          &lt;option value="2"&gt;Apartment&lt;/option&gt;
                          &lt;option value="3"&gt;Terrace&lt;/option&gt;
                          &lt;option value="4"&gt;Bungalow&lt;/option&gt;
                          &lt;/select&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Area: &lt;/td&gt;
                      &lt;td&gt;&lt;select name="Area" required="required"&gt;
                          &lt;option value&gt;Choose area&lt;/option&gt;
                          &lt;option value="1"&gt;Area 1&lt;/option&gt;
                          &lt;option value="2"&gt;Area 2&lt;/option&gt;
                          &lt;option value="3"&gt;Area 3&lt;/option&gt;
                          &lt;option value="4"&gt;Area 4&lt;/option&gt;
                          &lt;option value="5"&gt;Area 5&lt;/option&gt;
                          &lt;option value="6"&gt;Area 6&lt;/option&gt;
                          &lt;option value="7"&gt;Area 7&lt;/option&gt;
                          &lt;option value="8"&gt;Area 8&lt;/option&gt;
                          &lt;option value="9"&gt;Area 9&lt;/option&gt;
                          &lt;option value="10"&gt;Area 10&lt;/option&gt;
                          &lt;option value="11"&gt;Area 11&lt;/option&gt;
                          &lt;option value="12"&gt;Area 12&lt;/option&gt;
                          &lt;option value="13"&gt;Area 13&lt;/option&gt;
                          &lt;option value="14"&gt;Area 14&lt;/option&gt;
                          &lt;option value="15"&gt;Area 15&lt;/option&gt;
                          &lt;option value="16"&gt;Area 16&lt;/option&gt;
                          &lt;option value="17"&gt;Area 17&lt;/option&gt;
                          &lt;option value="18"&gt;Area 18&lt;/option&gt;
                          &lt;option value="19"&gt;Area 19&lt;/option&gt;
                          &lt;option value="20"&gt;Area 20&lt;/option&gt;
                          &lt;option value="21"&gt;Area 21&lt;/option&gt;
                          &lt;option value="22"&gt;Area 22&lt;/option&gt;
                          &lt;option value="23"&gt;Area 23&lt;/option&gt;
                          &lt;option value="24"&gt;Area 24&lt;/option&gt;
                          &lt;option value="25"&gt;Area 25&lt;/option&gt;
                          &lt;/select&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td valign="top"&gt;Address:&lt;/td&gt;
                          &lt;td&gt;&lt;textarea rows="1" cols="50" input type="text" name="home_add" width="400px" class="add" required="required"&gt;&lt;/textarea&gt;&lt;/td&gt;
                          &lt;tr&gt;
                          &lt;td valign="top"&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;textarea rows="1" cols="50" input type="text" name="home_add2" width="400px" class="add" required="required"&gt;&lt;/textarea&gt;&lt;/td&gt;
                          &lt;tr&gt;
                          &lt;td valign="top"&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;textarea rows="1"  input type="text" name="home_add3" class="add" &gt;&lt;/textarea&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                         &lt;td&gt;No. of room(s):&lt;/td&gt; 
                         &lt;td&gt;&lt;input type="number" name="no_room" width="60px" class="text2" required="required"&gt; &lt;/td&gt;
  
                     &lt;/tr&gt;
                      &lt;tr&gt;
                         &lt;td&gt;Air-condition:&lt;/td&gt;
  
                         &lt;td&gt;&lt;input type="radio" name="ac_avail" value="1"&gt;Yes &amp;nbsp; &amp;nbsp; &lt;input type="radio" name="ac_avail" value="0"&gt;No&lt;/td&gt;
  
                     &lt;/tr&gt;
                  &lt;/p&gt;
  
                  &lt;p&gt;
                  &lt;tr&gt;
                         &lt;td&gt;Contact No:&lt;/td&gt;
                         &lt;td&gt;&lt;input type="text" name="home_tel" width="300px" placeholder="Contact No." class="phone" required="required"&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr colspan=2&gt;
                      &lt;td&gt;Rental: &lt;/td&gt;
                      &lt;td&gt;&lt;b&gt;RM &amp;nbsp; &lt;/b&gt;&lt;input type="number" name="price" width="50px" placeholder="00.00"&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Deposit: &lt;/td&gt;
                      &lt;td&gt;&lt;b&gt;RM &amp;nbsp; &lt;/b&gt;&lt;input type="number" name="home_deposit" width="50px" placeholder="00.00" class="text"&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;/p&gt;
  
                  &lt;p&gt;
                  &lt;tr&gt;
                      &lt;td align="left"&gt;Images:&lt;/td&gt;
                      &lt;td&gt;&lt;input type="file" id="file" name="file[]" width="200px" height="200px" multiple="multiple"&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
  
              &lt;br&gt;
              &lt;input type="submit" name="postHome" value="POST" class="button large"&gt;
  &lt;/p&gt;
  &lt;/form&gt; 
  </code></pre>
  
  <p>when i try to upload it without the php of the image, its success but when i insert the image php it became -> Warning: Invalid argument supplied for foreach() </p>
  
  <p>php:</p>
  
  <pre><code>if(isset($_POST['postHome'])) //postHome name is taken from the name place for the submit button
      {       
          $userid=$_SESSION['oid'];
          $h_id=null;
          $h_title= $_POST['home_adTitle'];
          $h_name= $_POST['home_name'];
          $h_category= $_POST['home_category'];
          $h_add1= $_POST['home_add'];
          $h_add2= $_POST['home_add2'];
          $h_add3= $_POST['home_add3'];
          $h_area= $_POST['area'];
          $h_desc= $_POST['home_description'];
          $h_price= $_POST['price'];
          $h_deposit= $_POST['home_deposit'];
          $h_tel = $_POST['home_tel'];
          $h_ac = $_POST['ac_avail'];
          $h_room = $_POST['no_room'];
  
          $h_status = 1; //h_status: 1-available 0-unavailable
          $query = $conn-&gt;prepare("SELECT home_name FROM homestay WHERE home_name=:home_name");   //2: select table from db. if use //3: prepare statement
          $query-&gt;bindParam(':home_name', $h_name);
          $query-&gt;execute();
  
  
          if($query-&gt;rowCount() &gt; 0)
          {
  
              echo 'existed already';
          } 
          else 
          {
              // Prepaire SQL Statement
              $query = $conn-&gt;prepare("INSERT INTO homestay (home_id, home_name, ac_avail, no_room, price, home_category, home_tel, home_deposit, home_add, home_add2, home_add3, area, home_description, home_adTitle, status, o_id)             
              VALUES(null, :home_name, :ac_avail, :no_room, :price, :home_category, :home_tel, :home_deposit, :home_add, :home_add2, :home_add3, :area, :home_description, :home_adTitle, :status, :o_id)");
  
  
              $query-&gt;bindParam(':home_name', $h_name);
              $query-&gt;bindParam(':ac_avail', $h_ac);
              $query-&gt;bindParam(':no_room', $h_room);
              $query-&gt;bindParam(':price', $h_price);
              $query-&gt;bindParam(':home_category', $h_category);
              $query-&gt;bindParam(':home_tel', $h_tel);
              $query-&gt;bindParam(':home_deposit', $h_deposit);
              $query-&gt;bindParam(':home_add', $h_add1);
              $query-&gt;bindParam(':home_add2', $h_add2);
              $query-&gt;bindParam(':home_add3', $h_add3);
              $query-&gt;bindParam(':area', $h_area);
              $query-&gt;bindParam(':home_description', $h_desc);
              $query-&gt;bindParam(':home_adTitle', $h_title);
              $query-&gt;bindParam(':status', $h_status);
              $query-&gt;bindParam(':o_id', $userid);
  
  
              //IMAGE 
              function microtime_name()
              {
                  list($usec, $sec) = explode(" ", microtime());
                  return ($usec + $sec)*10000;            
              }
              $j=0;
              foreach($_FILES['file']['tmp_name'] as $key=&gt;$name_tmp)
              {
                  $target_path='home_image/';
                  // Loop to get individual element from the array
                  $validextensions = array("jpeg", "jpg", "png");      // Extensions which are allowed.
                  $ext = explode('.', basename($_FILES['file']['name'][$key]));   // Explode file name from dot(.)
                  $file_extension = end($ext); // Store extensions in the variable.
                  $namePic=microtime_name(); 
                  $target_path = $target_path . $namePic . "." . $ext[count($ext) - 1];     // Set the target path with a new name of image.
                  if (in_array($file_extension, $validextensions))//in_array check if value(in this case: $file_extension) exist in array or not(in this case: $validextensions)
                  {
                      if (move_uploaded_file($_FILES['file']['tmp_name'][$key], $target_path))
                      {
                          // If file moved to uploads folder.
                          //echo $j. ').&lt;span id="noerror"&gt;Image uploaded successfully!.&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;';
                          $image_name=$target_path;
                          $stmt=$conn-&gt;prepare("INSERT INTO h_image(image_id, image_name, home_id) VALUES (null, :image_name, null)");
                          $stmt-&gt;bindParam(':pic_path', $image_name);
                          echo $image_name.': &lt;br&gt;';
                          $stmt-&gt;execute();
                          //if($stmt)
                              echo 'success!&lt;br&gt;';
  
                      } 
                      else 
                      {     //  If File Was Not Moved.
                          echo $j. ').&lt;span id="error"&gt;please try again!.&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;';
                      }
                  } 
  
                  else 
                  {     //   If File Size And File Type Was Incorrect.
                      echo $j. ').&lt;span id="error"&gt;***Invalid file Size or Type***&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;';
                  }
              }
  
              if($query-&gt;execute())
              {
                  $h_id=$conn-&gt;lastInsertId('home_id');
              echo '&lt;br&gt;home id is:'.$h_id;
                  $test="select home_id from homestay";
                  $test=$conn-&gt;prepare($test);
                  $test-&gt;execute();
                  if($test-&gt;rowCount() &gt; 0)   
                  {
                  $r = $test-&gt;fetch(PDO::FETCH_LAZY);
  
                  }
  
              }
          }   
      }
  </code></pre>