February 06, 2021

How to create a Booking Calendar with Timeslots Php Mysql - 5

This is part 5 of How to create a Booking Calendar Php Mysql. In this tutorial we're going to do some validations before inserting bookings on mysql database.

Before inserting bookings in mysql, we will check that if the timeslot for that day is already there or not. If its already there we will show that the slot is already booked. But that's server side validation. We also do client side validation. To do this we will disable the button click on the timeslots which are already booked and disable it so clicking it will do nothing.

First we will create a function to get all the booked slots for the given date and save those slots in an array.

$mysqli = new mysqli('localhost', 'root', '', 'bookingcalendar');
if(isset($_GET['date'])){
    $date = $_GET['date'];
    $stmt = $mysqli->prepare("select * from bookings where date = ?");
    $stmt->bind_param('s', $date);
    $bookings = array();
    if($stmt->execute()){
        $result = $stmt->get_result();
        if($result->num_rows>0){
            while($row = $result->fetch_assoc()){
                $bookings[] = $row['timeslot'];
            }
            $stmt->close();
        }
    }
}

Then while display all timeslots, we will check if the created timeslot is available in the bookings array. If it is available, that means its booked. So we have to disable it.

<div class="row">
<div class="col-md-12">
   <?php echo(isset($msg))?$msg:""; ?>
</div>
<?php $timeslots = timeslots($duration, $cleanup, $start, $end); 
    foreach($timeslots as $ts){
?>
<div class="col-md-2">
    <div class="form-group">
       <?php if(in_array($ts, $bookings)){ ?>
       <button class="btn btn-danger"><?php echo $ts; ?></button>
       <?php }else{ ?>
       <button class="btn btn-success book" data-timeslot="<?php echo $ts; ?>"><?php echo $ts; ?></button>
       <?php }  ?>
    </div>
</div>
<?php } ?>
</div>

Finally, we have to do server side validation to make sure that same slot cannot be booked twice.

if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $timeslot = $_POST['timeslot'];
    $stmt = $mysqli->prepare("select * from bookings where date = ? AND timeslot=?");
    $stmt->bind_param('ss', $date, $timeslot);
    if($stmt->execute()){
        $result = $stmt->get_result();
        if($result->num_rows>0){
            $msg = "<div class='alert alert-danger'>Already Booked</div>";
        }else{
            $stmt = $mysqli->prepare("INSERT INTO bookings (name, timeslot, email, date) VALUES (?,?,?,?)");
            $stmt->bind_param('ssss', $name, $timeslot, $email, $date);
            $stmt->execute();
            $msg = "<div class='alert alert-success'>Booking Successfull</div>";
            $bookings[] = $timeslot;
            $stmt->close();
            $mysqli->close();
        }
    }
}

Related Videos
How to create a Booking Calendar Php Mysql - 1
How to create a Booking Calendar Php Mysql - 2
How to create a Booking Calendar with Timeslots Php Mysql - 3
How to create a Booking Calendar with Timeslots Php Mysql - 4