<?php
//update.php

include('connect.php');
if (isset($_POST['image_id'])) {
    
    $image_id = $_POST['image_id'];

    if (!empty($_FILES['file_name']['name'])) {

       $fileTmp = $_FILES['file_name']['tmp_name'];

       $allowImg = array('png','jpeg','jpg','gif');

       $fileExnt = explode('.', $_FILES['file_name']['name']);

       $fileActExt   = strtolower(end($fileExnt));

       $newFile = rand(). '.'. $fileActExt;

       $destination = 'uploads/hotels/'.$newFile;

       if (in_array($fileActExt, $allowImg)) {
          if ($_FILES['file_name']['size'] > 0 && $_FILES['file_name']['error']==0) {

              $query = "SELECT * FROM tbl_hotelimages WHERE himg_id = '$image_id'";

              $result = mysqli_query($con, $query);

              $row = mysqli_fetch_assoc($result);

              $filePath = 'uploads/hotels/'.$row['hotel_img'];
              
              if (move_uploaded_file($fileTmp, $destination)) {
                $update = "UPDATE tbl_hotelimages SET hotel_img = '$newFile' WHERE himg_id = '$image_id'";
                mysqli_query($con, $update);
                unlink($filePath);
              }
          }
       }
    }

  }
  
 

//placesimages update

  if (isset($_POST['image_id1'])) {
    
      $image_id = $_POST['image_id1'];

       if (!empty($_FILES['file_name']['name'])) {

       $fileTmp = $_FILES['file_name']['tmp_name'];

       $allowImg = array('png','jpeg','jpg','gif');

       $fileExnt = explode('.', $_FILES['file_name']['name']);

       $fileActExt   = strtolower(end($fileExnt));

       $newFile = rand(). '.'. $fileActExt;

       $destination = 'uploads/places/'.$newFile;

       if (in_array($fileActExt, $allowImg)) {
              if ($_FILES['file_name']['size'] > 0 && $_FILES['file_name']['error']==0) {

                $query = "SELECT * FROM tbl_placesimages WHERE img_id = '$image_id'";

                $result = mysqli_query($con, $query);

                $row = mysqli_fetch_assoc($result);

                $filePath = 'uploads/places/'.$row['file_name'];
                
                if (move_uploaded_file($fileTmp, $destination)) {
                    $update = "UPDATE tbl_placesimages SET file_name = '$newFile' WHERE img_id = '$image_id'";
                    mysqli_query($con, $update);
                    unlink($filePath);
                }
             }
      }
     }

  }
  
 

?>
