Overlap of time slot

i have made a booking system where user can select time slot enter start & end time & save those values into MySql. Now i have found an issue where User A has booked time slot from 3-4. Now User B wants to book time slot 2.30-4.30. At present it is allowing User B to select the time slot which i don’t want.Tried to code for it but not getting the desired result.
Please Help!

    <?php 
    session_start();
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password 
    $db_name="testmra"; // Database name 

    // Connect to server and select databse.
    $conn=mysqli_connect($host,$username,$password) or die("cannot connect"); 

    mysqli_select_db($conn,$db_name);
    $sname=$_SESSION['usr_name'];


    $room = mysqli_real_escape_string($conn, $_POST['txtrname']);
    $name = mysqli_real_escape_string($conn, $_POST['txtname']);
    $dept = mysqli_real_escape_string($conn, $_POST['txtdept']);
    $purpose = mysqli_real_escape_string($conn, $_POST['txtpurpose']);
    $attendee = mysqli_real_escape_string($conn, $_POST['attendee']);
    $date = mysqli_real_escape_string($conn, $_POST['txtdate']);
    $btime = mysqli_real_escape_string($conn, $_POST['btime']);
    $etime = mysqli_real_escape_string($conn, $_POST['etime']);

    $overlap="SELECT * FROM karmadetails WHERE date = '$date' AND status_id=2";
 $result1=mysqli_query($overlap);
 $num1=mysqli_num_rows($result1);

$i=0;
$overflowed=false;
while ($i < $num1)
{
    $MinValue=mysqli_result($result1,$i,$btime);
    $MaxValue=mysqli_result($result1,$i,$etime);
   if (($StartTime < $MaxValue) && ($MinValue < $EndTime)) {
    $overflowed=true;
    break;
}
$i++;
}
if ($overflowed)
{
   echo '<script language="javascript">alert("Please select another time slot")</script>';
}
else
{
    $sql="INSERT INTO bookingdetails (room,name,department,purpose,attendee,date,starttime,endtime,status_id)VALUES('$room','$name','$dept','$purpose','$attendee','$date','$btime','$etime','2')";

    $res  = mysqli_query($conn,"SELECT emailid FROM newuser WHERE username='$sname'");

    $row = mysqli_fetch_assoc($res);
    $to = $row["emailid"];

    require('phpmailer/PHPMailerAutoload.php');

    $mail = new PHPMailer();

    $subject = "Bookig Details";
    $content = "<b>Hello $name. Your Booking Details are as follow. Room : $room Date : $date Start Time : $btime End Time : $etime</b>";
    $mail->IsSMTP();
    $mail->SMTPDebug = 0;   
    $mail->SMTPAuth = TRUE; 
    $mail->SMTPSecure = "no";
    $mail->Port     = 26;  
    $mail->Username = "admin";
    $mail->Password = "@@@@";
    $mail->Host     = "59.68.1.101";
    $mail->Mailer   = "smtp";
    $mail->SetFrom("admin@hitechplast.in", "Admin");
    $mail->AddReplyTo("admin@hitechplast.in", "Admin");
    $mail->AddAddress($to);
    $mail->Subject = $subject;
    $mail->WordWrap   = 80;
    $mail->MsgHTML($content);
    $mail->IsHTML(true);

    if(!$mail->Send())
  {
     echo "Message could not be sent. <p>";
    echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
}

    if (mysqli_query($conn,$sql))
      {
        echo "Record added";

        }
    else
    {
        die('Error: ' . mysqli_error());
     }
?> 


Source: mysql

Leave a Reply