<?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(isset($_POST['itm_img_up']))
		{
			$id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');
			$sub_title = htmlspecialchars(trim($_POST['sub_title_up']), ENT_QUOTES, 'UTF-8');
			$main_cat = htmlspecialchars(trim($_POST['main_cat_up']), ENT_QUOTES, 'UTF-8');
			$description = htmlspecialchars(trim($_POST['description_up']), ENT_QUOTES, 'UTF-8');
			
			$valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp');
			$path = '../upload/export_banner/'; // upload directory
			$img = htmlspecialchars(trim($_FILES['itm_img_up']['name']), ENT_QUOTES, 'UTF-8');
			$tmp = htmlspecialchars(trim($_FILES['itm_img_up']['tmp_name']), ENT_QUOTES, 'UTF-8');

			// get uploaded file's extension
			$ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));

			// can upload same image using rand function
			$final_image = rand(1000,1000000).$img;

			// check's valid format
			if(in_array($ext, $valid_extensions))
			{
			$path = $path.strtolower($final_image); 
			//echo "img logo name".$final_image;
			if(move_uploaded_file($tmp,$path))
			{
			  $result = mysqli_query($conn, "UPDATE export_banner SET img_name='$final_image', sub_title='$sub_title', main_title='$main_cat', description='$description' WHERE id='$id'");
			  if ($result) {
			    echo "Data inserted";
			  }
			}
			}
			else 
			{
			echo 'invalid';
			}
		}else{

			$id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');
			$sub_title = htmlspecialchars(trim($_POST['sub_title_up']), ENT_QUOTES, 'UTF-8');
			$main_cat = htmlspecialchars(trim($_POST['main_cat_up']), ENT_QUOTES, 'UTF-8');
			$description = htmlspecialchars(trim($_POST['description_up']), ENT_QUOTES, 'UTF-8');
			  $result = mysqli_query($conn, "UPDATE export_banner SET sub_title='$sub_title', main_title='$main_cat', description='$description' WHERE id='$id'");
			  if ($result) {
			    echo "Data inserted without image";
			  }else{
				echo 'invalid without image';
				}
		}

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

?>