title: Inserting multiple images in MySQL database using for each loop
body: <p>I need to insert multiple images in the database in the following format,
  <code>09823754257b41f19e24c01471.jpg, 754257b41f19e24c01471422234.jpg,</code></p>
  
  <p>I have written the following code to insert the value in the database</p>
  
  <pre><code>&lt;?php include "dbconnect.php";
  
  if(isset($_POST['property_add'])){  
  $propTitle  = $_POST['title'];
  $propSize   = $_POST['size'];
  $propType   = $_POST['type'];
  $propLoc    = $_POST['location'];
  $propStatus = $_POST['status'];
  $propPrice  = $_POST['price'];
  $propDesc   = $_POST['propDesc'];
  
  if(isset($_FILES['files'])){
      foreach($_FILES['files']['tmp_name'] as $key =&gt; $tmp_name ){
          $temp = explode(".", $_FILES['files']['name'][$key]);
          $file_name = rand().uniqid().round(microtime(true)) . '.' . end($temp);
          $file_size =$_FILES['files']['size'][$key];
          $file_tmp =$_FILES['files']['tmp_name'][$key];
          $file_type=$_FILES['files']['type'][$key];  
  
          $desired_dir="../property/".$propTitle."/".$propStatus."/";
          if(is_dir($desired_dir)==false){
              mkdir("$desired_dir", 0777, true);      // Create directory if it does not exist
          }
          if(is_dir("$desired_dir/".$file_name)==false){
              move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
          }else{                                  //rename the file if another one exist
              $new_dir="$desired_dir/".$file_name.time();
              rename($file_tmp,$new_dir) ;                
          }
          if($tmep){
            $photos[] = $file_name;
          }
      }
  
      $files  =implode(',', $photos);
      $query  =  "INSERT INTO `property`(`property_name`, `property_size`, `property_type`,`property_loc`, `property_status`, `property_price`, `property_desc`, `property_photos`) VALUES ('$propTitle','$propSize','$propType','$propLoc','$propStatus','$propPrice','$propDesc','$files')";
      mysqli_query($conn,$query);
      echo "&lt;script&gt;alert('Property Added Successfully')&lt;/script&gt;";
      header('Location:../dashboard.php?tab=add&amp;mode=success');
  }
  }
  </code></pre>
  
  <p>Here the problem is if I selected two images <code>img1</code> and <code>img2</code> but the above code generates the following value for the files</p>
  
  <p><code>09823754257b41f19e24c01471.jpg, 754257b41f19e24c01471422234.jpg,8237542e24c0171422252. ,</code></p>
  
  <p>I want the two values only inserted in the database, I don't known why the third value generated.</p>