lundi 20 avril 2015

Prevent double booking between two times php pdo

I have set up a form where my users can log in and book a facility through the use of a form. They choose a venue, date, time and other options. I have set this code up to try and prevent the users booking the same time in the same day. I have a separate register form where my PHP code checks for the same user in the database and if that user exists it gives an error code. My PHP code for my booking form doesn't preform this check. I also want to check values inside booking times e.g. if a user has already booked a slot for 20th April at 9AM - 11AM I want to prevent someone booking the 10AM slot between these two times. My PHP code is submitted below and I will also enclose my HTML form.

    <div class = "booking">
                <form class="form-horizontal" role="form" method="post" action="">
                
                        <div class="form-group">
                                <label for="pitchname" class="col-sm-2 control-label">Pitch Name:</label>
                                <div class="col-sm-10">
                                        <select name="pitchID" id="pitchID">
                                                <?php
                                                        
                                                        include "config.php";
                                                        
                                                        $dbQuery = $db->prepare("SELECT pitchID, pitch_name FROM pitch ORDER BY pitchID asc"); 
                                                        $dbQuery->execute(); 
                                                        
                                                        while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) 
                                                        { 

                                                                echo '<option value= "'. $dbRow['pitchID'] . ' ">' . $dbRow['pitch_name'] . '</option>'; 
                                                        }
                                                         
                                                ?>
                                        </select>
                                </div>
                        </div>
                        
                        <div class="form-group">
                                <label class="col-sm-2 control-label">Start Date:</label>
                                <div class="col-sm-10">
                                        <input class="form-control" type="date" id="datepicker" name="start_date" required="required">
                                </div>
                        </div>

                        <div class="form-group">
                                <label for="start_hour" class="col-sm-2 control-label">Start Hour:</label>
                                <div class="col-sm-10">
                                        <select name="start_hour" id="start_hour" onchange="" size="1">
                                                <option value="09:00:00">09:00</option>
                                                <option value="10:00:00">10:00</option>
                                                <option value="11:00:00">11:00</option>
                                                <option value="12:00:00">12:00</option>
                                                <option value="13:00:00">13:00</option>
                                                <option value="14:00:00">14:00</option>
                                                <option value="15:00:00">15:00</option>
                                                <option value="16:00:00">16:00</option>
                                                <option value="17:00:00">17:00</option>
                                                <option value="18:00:00">18:00</option>
                                                <option value="19:00:00">19:00</option>
                                                <option value="20:00:00">20:00</option>
                                                <option value="21:00:00">21:00</option>
                                        </select>
                                </div>
                        </div>
                        
                        <div class="form-group">
                                <label for="end_hour" class="col-sm-2 control-label">End Hour:</label>
                                <div class="col-sm-10">
                                        <select name="end_hour" id="end_hour" onchange="" size="1">
                                                <option value="09:00:00">09:00</option>
                                                <option value="10:00:00">10:00</option>
                                                <option value="11:00:00">11:00</option>
                                                <option value="12:00:00">12:00</option>
                                                <option value="13:00:00">13:00</option>
                                                <option value="14:00:00">14:00</option>
                                                <option value="15:00:00">15:00</option>
                                                <option value="16:00:00">16:00</option>
                                                <option value="17:00:00">17:00</option>
                                                <option value="18:00:00">18:00</option>
                                                <option value="19:00:00">19:00</option>
                                                <option value="20:00:00">20:00</option>
                                                <option value="21:00:00">21:00</option>
                                        </select>
                                </div>
                        </div>
                                
                        <div class="form-group">
                                <label for="booking_age" class="col-sm-2 control-label">Booking Age:</label>
                                <div class="col-sm-10">
                                        <select name="booking_age" id="booking_age" onchange="" size="1">
                                                <option value="0">Adult</option>
                                                <option value="1">Junior (U18's)</option>
                                        </select>
                                </div>
                        </div>
                                
                        <div class="form-group">
                                <label for="pitch_size" class="col-sm-2 control-label">Pitch Size:</label>
                                <div class="col-sm-10">
                                        <select name="pitch_size" id="pitch_size" onchange="" size="1">
                                                <option value="0">Full Pitch</option>
                                                <option value="1">Half Pitch</option>
                                        </select>
                                </div>
                        </div>
                        
                        <div class="form-group">
                                <label for="light_tokens" class="col-sm-2 control-label">Light Tokens?:</label>
                                <div class="col-sm-10">
                                        <select name="light_tokens" id="light_tokens" onchange="" size="1">
                                                <option value="0">Yes</option>
                                                <option value="1">No</option>
                                        </select>
                                </div>
                        </div>
                                
                        
                        <div class="form-group">
                        <div class="col-sm-10">
                                <input id="button" name="booking" type="submit" value="Book" class="btn btn-primary">
                        </div>    
                        </div> <!-- booking button --> 
                
                </form>
        </div>
    <?php
                include "config.php"; 
                
                //Booking point
                if(isset($_POST['booking']))
                {
                        //get values for variables
                        $pitchID = $_POST['pitchID'];
                        $start_date = $_POST['start_date'];
                        $start_hour = $_POST['start_hour'];
                        $end_hour = $_POST['end_hour'];
                        $booking_age = $_POST['booking_age'];
                        $pitch_size = $_POST['pitch_size'];
                        $light_tokens = $_POST['light_tokens'];
                        
                        $q = $db->prepare("INSERT INTO booking SET pitchID = ?, start_date = ?, start_hour = ?, end_hour = ?, booking_age = ?, pitch_size = ?, light_tokens = ?");
                        $query = $q->execute(array($pitchID, $start_date,$start_hour,$end_hour,$booking_age,$pitch_size,$light_tokens));
                        
                        $count = $q->rowCount();
                                if($count == 0)
                                {
                                        echo "Your booking has been made";
                                        header("Location:home2_template.html");
                                        return; 
                                }else {
                                        echo "That booking already exists";
                                }
                }
        
        ?>

Aucun commentaire:

Enregistrer un commentaire