<?php
// initializing variables
$pname = "";
$image = "";
$errors = array(); 
// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'shopping');

// UPLOAD IMAGE
if(isset($_POST['submit_button'])) {
  // receive all input values from the form
  $pname = mysqli_real_escape_string($db, $_POST['pname']);

  $image   = isset($_POST['image'])?$_POST['image']:'';
  $uploads_dir = 'uploads/banners';
  $img_path = 'http://localhost/folder/uploads/banners/';
  $tmp_name = $_FILES["image"]["tmp_name"];
  $name = basename($_FILES["image"]["name"]);
  move_uploaded_file($tmp_name, "$uploads_dir/$name");
  $p_img_url =  $img_path.$name;    
  if(!empty($name))
   {
    $p_img_url = $img_path.$name;
   }
   else
    {
      $p_img_url = '';
    }

  if (empty($pname)) { array_push($errors, "Name is required"); }
  
  // first check the database to make sure 
  // a user does not already exist with the same username and/or email

  // Finally, register user if there are no errors in the form
  if (count($errors) == 0) {
    
    $query = "INSERT INTO banners(banner_name,image_dir) VALUES ('$pname','$p_img_url')";

  if (mysqli_query($db,$query)) 
  {
    $_SESSION['pname'] = $pname;
    echo '<script>Swal.fire("Banner Added","Your data is Submitted","success");</script>'; 
  }
 }
}
?>