<?php
include '../db/db.php';
session_start();
$log=htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
if (empty($log)) {
     
  $redirectUrl = "../index.php";
  echo "<script type=\"text/javascript\">";
  echo "window.location.href = '$redirectUrl'";
  echo "</script>";
	 
}else{

  $sqlu="SELECT * FROM details_of_users WHERE user='$log' and val='1'";
  $resultu=mysqli_query($conn, $sqlu);
  $countu=mysqli_num_rows($resultu);
  if($countu==1){

    if (array_key_exists('del_main_banner', $_POST)) {

      $id=htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');

      $sqlc="SELECT img_name FROM main_banner WHERE id = '$id'";
      $resultc=mysqli_query($conn, $sqlc);
      while ($rowc = mysqli_fetch_array($resultc)){
        $img_name=$rowc['img_name'];
        unlink('../upload/banner/'.$img_name);
      }

      $sql2=mysqli_query($conn,"DELETE FROM main_banner WHERE id = '$id'");

    	if ($sql2) {
        echo "Data Deleted";
      }else{
        echo "Invalid";
      }

    }

    if (array_key_exists('del_banner_sub1', $_POST)) {
      $id=htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');

      $sqlc="SELECT img FROM banner2 WHERE id = '$id'";
      $resultc=mysqli_query($conn, $sqlc);
      while ($rowc = mysqli_fetch_array($resultc)){
        $img=$rowc['img'];
        unlink('../upload/banner2/'.$img);
      }

      $sql2=mysqli_query($conn,"DELETE FROM banner2 WHERE id = '$id'");

      if ($sql2) {
        echo "Data Deleted";
      }else{
        echo "Invalid";
      }
    }

    if (array_key_exists('del_banner_sub2', $_POST)) {
      $id=htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');

      $sqlc="SELECT img FROM banner3 WHERE id = '$id'";
      $resultc=mysqli_query($conn, $sqlc);
      while ($rowc = mysqli_fetch_array($resultc)){
        $img=$rowc['img'];
        unlink('../upload/banner3/'.$img);
      }

      $sql2=mysqli_query($conn,"DELETE FROM banner3 WHERE id = '$id'");

      if ($sql2) {
        echo "Data Deleted";
      }else{
        echo "Invalid";
      }
    }

    if (array_key_exists('del_export_banner', $_POST)) {
      $id=htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');

      $sqlc="SELECT img_name FROM export_banner WHERE id = '$id'";
      $resultc=mysqli_query($conn, $sqlc);
      while ($rowc = mysqli_fetch_array($resultc)){
        $img=$rowc['img_name'];
        unlink('../upload/export_banner/'.$img);
      }

      $sql2=mysqli_query($conn,"DELETE FROM export_banner WHERE id = '$id'");

      if ($sql2) {
        echo "Data Deleted";
      }else{
        echo "Invalid";
      }
    }

  } else{

    $redirectUrl = "../index.php";
    echo "<script type=\"text/javascript\">";
    echo "window.location.href = '$redirectUrl'";
    echo "</script>";

  }
}
?>