我已经建立了一个预订系统,用户可以选择时段输入start&结束时间&将这些值保存到MySql中。现在我发现了一个问题,用户A已经预订了3-4的时间段。现在用户B想要预订时间段2.30-4.30。目前,它允许用户B选择我不想要的时间段。必须编写代码才能获得所需的结果。 请帮助!
<?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']);
$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 starttime,endtime FROM bookingdetails WHERE date = '$date' AND room='$room' ";
$result=mysqli_query($query);
$num=mysqli_num_rows($result);
$i=0;
while ($i <= $num)
{
$MinValue=mysqli_result($result,$i,"starttime");
$MaxValue=mysqli_result($result,$i,"endtime");
if (($btime < $MaxValue) && ($MinValue < $etime)) {
$overflowed=true;
}
$i++;
}
if ($overflowed)
{
//Error message
header("location:karma.php");
echo '<script language="javascript">';
echo 'alert(" Time Slot is overlapping.Change the time slot")';
echo '</script>';
}
else
{
//Save to database
$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());
}
?>
答案 0 :(得分:1)
您可以执行计算现有重叠的SQL请求:
SELECT count(*) WHERE $requested_start < endtime
AND $requested_end > starttime
AND date = $date
AND status_id = 2;
现在可以使用此结果检查时隙是否可用:
$overlappings = mysqli_fetch_row($result);
// If there is no overlapping
if($overlappings[0] <= 0){
// time slot is available
}
答案 1 :(得分:0)
<?php
$sql = "select room_no FROM roominfo WHERE room_no NOT IN (SELECT roomno FROM guestrocordtransac WHERE '".$format_checkin."' between checkin AND checkout and roomtype='".$roomtype."' UNION SELECT roomno FROM guestrocordtransac WHERE '".$format_checkout."' BETWEEN checkin AND checkout and roomtype='".$roomtype."') and roomtype='".$roomtype."' ";
$retval = mysql_query( $sql, $conn );
if(mysql_num_rows($retval)== 0 ||mysql_num_rows($retval)== null )
{
?>
<script type="text/javascript">
alert("Please select another date/Type")</script>
<?php
}
else{ ///inserrt code///}
这是一个示例代码...用您的时间替换格式checkin ...我认为这可能对您有帮助..