Jump to content

PHP conditional statement (prevent a certain record from being inserted into database)


Recommended Posts

I have tried everything to prevent this scenario from happening, but everything has failed. I would be very grateful if anyone could help with this scenario. If someone has booked an equipment on a certain day between 00:00 and 06.00, then someone tries to book the same equipment on the same day between 01:00 and 05:00, how do I stop this from happening? No matter what I do, the system still inserts a record into the database.Here is the code I am using:

 

    <?php
    if(isset($_GET['add'])){
    $equipment=$_POST['equipment'];
    $start=$_POST['start'];
    $end=$_POST['end'];
    $notes=mysqli_real_escape_string($dbconnection, $_POST['notes']);
    $eventdate=$month."/".$day."/".$year;

    //This code inserts the record
    $sqlinsert="insert into bookings (equipment,start_time,end_time,notes,selected_date,date_added) values ('".$equipment."','".$start."','".$end."','".$notes."','".$eventdate."',now())";
    $resultinsert=mysqli_query($dbconnection,$sqlinsert);

    if($resultinsert){
    echo "<span class='go'>  Booking was successful  </span>";
    }
    }


    ?>

 

 

 

I have tried everything: from asking the database to check for records: "SELECT * FROM bookings WHERE selected_date BETWEEN start_time AND end_time";

 

to using conditional statements: if($start>$start && $end<$end) { echo "no"; }

 

But nothing is working. Thank you to anyone who can help.

Your logic is very much incorrect.

 

I guess bookings can't span multiple days? Look for bookings on the same day where the start time is before the requested end time and the end time is after the requested start time.

@requinix

 

I tried doing the following, but what ended up happening is that no bookings were allowed to be made.

 

      $sql="SELECT * FROM bookings WHERE selected_date AND start_time < end_time AND end_time>start_time";
     $result= mysqli_query($dbconnection, $sql);
     $results=mysqli_num_rows($result);
     if($results>0) {
         echo "<span class='no'> Error </span>";
     }else{
    
     //This code inserts the record
     $sqlinsert="insert into bookings (equipment,start_time,end_time,notes,selected_date,date_added) values ('".$equipment."','".$start."','".$end."','".$notes."','".$eventdate."',now())";
     $resultinsert=mysqli_query($dbconnection,$sqlinsert);

     if($resultinsert){
         echo "<span class='go'>  Booking was successful  </span>";
     }

 

 

What I am building is a calendar. Yes, bookings are not allowed to span multiple days. The user clicks a date and then has to select an equipment from a drop down list along with the booking times (also from drop down lists- start time and end time have their own drop down lists.) So if the user has selected 12.00 to 18.00 and then another user tries booking the same equipment on the same day from 14.00 to 15.00 (obvious time clash) the system unfortunately still goes ahead and books the second user's request.

 

I just don't know what to do to stop this from happening.

You can stop this from happening by understanding what you're doing and thus realizing you're still wrong about it.

WHERE selected_date
Explain to me what, precisely, that means. Not what you want it to mean but what it actually means.

@requinix-

 

selected_date is the field in the database- used to store the date which the user has selected from the calendar.

 

I used the following variable to be store the date.

 

$eventdate=$month."/".$day."/".$year;

 

The $eventdate variable is then inserted into the selected_date field in the bookings table in the database.

Edited by hgkhkhk
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.