sports-arena-website / badminton_booking_insert.php
badminton_booking_insert.php
Raw
<?php

include("conn.php");

//to check for member session
session_start();
// if don't have session user..then
if(! isset($_SESSION['memname']))
{
session_write_close();
die("<script>alert('Please login first before booking courts!');window.location.href='login.html';</script>");
}

//To check whether the duration selected is compatible with the start time and end time
$duration= $_POST['duration'];

$date1 = $_POST['starttime'];
$date2 = $_POST['endtime'];

$diff = round((strtotime($date2) - strtotime($date1))/(60*60));

if ($diff != $duration)
{
    echo "<script>alert('Please make sure that the duration selected is correct with the start time and end time!');</script>";
	die("<script>window.history.go(-1);</script>");
}

//To check that the booking is on future not the past
$date = $_POST['starttime'];
$datenow = date('Y-m-d H:i a');
if ($date < $datenow) {
	echo "<script>alert('Please make sure that your booking date time is valid!'); </script>";
	die("<script>window.history.go(-1);</script>");
}

//To prevent double booking of the court at same time
$courtnumber = $_POST['cid'];
$bookingstarttime = $_POST['starttime'];
$bookingendtime = $_POST['endtime'];

$checkBookingQuery = "SELECT * FROM `badminton_court_booking` WHERE `Court_ID` = {$courtnumber} AND (Start_Time >= '{$bookingstarttime}' AND End_Time <= '{$bookingendtime}')";
$checkBooking = mysqli_query($conn, $checkBookingQuery);

if(mysqli_num_rows($checkBooking)>0) {
	echo "<script>alert('Sorry the court is booked by other during that period of time! Please try with other date time/courts!'); </script>";
	die("<script>window.history.go(-1);</script>");	
}



//Insert data into databae
$sql="INSERT INTO badminton_court_booking (Mem_ID, Court_ID, Start_Time, End_Time, Duration)

VALUES

('$_POST[memid]','$_POST[cid]','$_POST[starttime]','$_POST[endtime]','$_POST[duration]');";

if(!mysqli_query($conn, $sql))
	{
	die('Error: '.mysqli_error($conn));
	}

echo '<script>alert("You have been successfully booked the court!");
window.location.href = "booking_history.php";
</script>';

mysqli_close($conn);
?>