<?php 
  if (!isset($_SESSION['username'])) {
    $_SESSION['msg'] = "To access this page, Login is required";
    header('location: login.php');
  }  ?>

<?php
  // Initialize message variable
$errors = array(); 

$p_name= "";
$p_price= "";
$quantity = "";
$p_size= "";
$description= "";

// If upload button is clicked ...
  if (isset($_POST['upload'])) {
    // Get image name
    $image = $_FILES['image']['name'];
    // Get text
   $category = mysqli_real_escape_string($db, $_POST['category']);
   $p_name = mysqli_real_escape_string($db, $_POST['p_name']);
   $p_size = mysqli_real_escape_string($db, $_POST['p_size']);
   $quantity = mysqli_real_escape_string($db, $_POST['quantity']);
   $p_price = mysqli_real_escape_string($db, $_POST['p_price']);
   $description = mysqli_real_escape_string($db, $_POST['description']);

    // image file directory
    $target = "../product-images/".basename($image);

   $username=$_SESSION['username'];


   

    $sql = "INSERT INTO products (Image_name, Category, P_name, P_size,  Quantity,   P_price, Description, Session ) 
    VALUES ('$image', '$category', '$p_name',  '$p_size', '$quantity', '$p_price', '$description', '$username')";
    // execute query

    
    if ((move_uploaded_file($_FILES['image']['tmp_name'], $target)) AND ($db->query($sql) === TRUE)  ) {
     echo "<script type= 'text/javascript'>alert('succesfully uploaded.');</script>";
     // echo "<script type='text/javascript'>location.href = 'index.php?link=store-updates';</script>";
    }else{
      echo "<script type= 'text/javascript'>alert('Error !  Unable to insert  [$p_name]  records.');</script>";
    }
  }
  $result = mysqli_query($db, "SELECT * FROM stores");
?>
