时间段重叠

时间:2015-06-22 07:30:58

标签: php mysql

我正在开发"会议室"用户选择时间段来预订房间的项目。对于插入过程,我已经编写了查询但是我已经编写了查询以检查时隙的重叠。它在第一次工作正常,但每当我在同一会话中检查下一次时,它会给出时隙和时间重叠的消息。不允许插入数据。 这是我的 php 代码段: -

<?php
session_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // 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']);
$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']);

$query = "SELECT count(*) as  c from bookingdetails WHERE starttime<='$etime' AND endtime>='$btime' AND date = '$date' AND status_id='2'";

$result = mysqli_query($conn, $query);

$overlappings =mysqli_fetch_array($result);

// If there is no overlapping
if($overlappings[0] <= 0) {

    // INSERT QUERY HERE
    $sql="INSERT INTO bookingdetails (room,name,purpose,attendee,date,starttime,endtime,status_id)VALUES('$room','$name','$purpose','$attendee','$date','$btime','$etime','2')";

    if (mysqli_query($conn,$sql))
      {
        //echo "Record added";
        }
    else
    {
        die('Error: ' . mysqli_error());
    }
} else {
    echo 'Time Slot is overlapping.Change the time slot';    
}
?>

0 个答案:

没有答案