
<?php 

include('../conn.php');
$page='Order submit';
include('../common/head.php');

if(isset($_POST['submit'])){
 
    $name=addslashes($_POST['name']);
     $number=$_POST['contact'];
    $payment=$_POST['payment'];
     $orderid=$_POST['orderid'];
     $order_id=$_POST['order_id'];
     $date=$_POST['date'];
     $dates=$_POST['dates'];
     $item_total=$_POST['item_total'];
     $food_name=$_POST['food_name'];
     $quantity=$_POST['quantity'];
     $price=$_POST['price'];
     $total=$_POST['total'];
     $location=$_POST['location'];
     $by=$_POST['by'];
     $time=$_POST['time'];
     $item_id=$_POST['item_id'];
     $order_type=$_POST['order_type'];

     $sql = "SELECT `number` FROM `customers` WHERE `number`='".$number."'";
     $result = $conn->query($sql);
     
     if($result->num_rows >= 1) {

      
     } else {
		// Image db insert sql
		$sql = "INSERT INTO `customers`(`fname`, `number`, `date`, `by`) VALUES ('$name','$number','$date','$by')";
		if(mysqli_query($conn,$sql)){


		}
		else{
		  echo 'Error: '.mysqli_error($conn);
		}
 
     }
     
     $sql1 = "INSERT INTO `orders_details`( `orderid`, `costomer_name`, `contact`, `amount`, `payment_method`, `date`, `time`,`by`)
     VALUES ('$order_id','$name','$number','$total','$payment','$date','$time','$by')";
     if(mysqli_query($conn,$sql1)){ 
$total=count($_POST['item_id']);
for ($i=0; $i <$total ; $i++) { 
$sql2="INSERT INTO `orders`(`orderid`,`item_id`,`item_name`,`quantity`, `price`, `total`,  `location`, `date`) VALUES('$orderid[$i]','$item_id[$i]','$food_name[$i]','$quantity[$i]','$price[$i]','$item_total[$i]','$location[$i]','$dates[$i]')";
if(mysqli_query($conn,$sql2)){
    unset($_SESSION['cart']);
    echo"<script>alert('Print reciept'); window.location.href='../print/order_print.php?item=$order_id'</script>";

  
}
else{
    echo 'Error: '.mysqli_error($conn);
  }


}}

   
}

?>
      


      