简体   繁体   English

如何提供合适的查询以检查房间的可用性

[英]How to provide a suitable query to check room availability

I am trying to create a hotel room booking app .... I have created two tables 我正在尝试创建一个酒店房间预订应用程序....我已经创建了两个表

  • the room table which consists of roomno(PK), room type and room price 由房间编号(PK),房间类型和价格组成的房间表

  • the availability table which consists of roomno(FK), room type, check-in-date and check-out-date 可用性表,其中包括roomno(FK),房间类型,入住日期和退房日期

the PHP code I have created so far allows me to check the availability for a particular type of room for the given dates ... 到目前为止,我已经创建的PHP代码使我可以检查给定日期特定类型房间的可用性...

$type = $_POST['pincode'];
$cin = $_POST['cin'];
$cout = $_POST['cout'];

$sql="select * from avalability where type = '$type' and cout>='$cin' and cin<='$cout'";
$res=mysqli_query($con,$sql);
$check=mysqli_fetch_array($res);
if(isset($check))
{
echo "rooms not available";
}
else
{
echo "rooms available";
$insert="insert into login.avalability(type,cin,cout) values('$type','$cin','$cout')";
mysqli_query($con,$insert);
}
mysqli_close($con);
?>

Now my question: suppose I have 5 single rooms in my room table and one single room has been booked for say 23rd march to 27th march, this makes the other four rooms free ... 现在我的问题是:假设我的房间桌子上有5个单人间,其中一个单人间已预订为3月23日至3月27日,这使得其他4个房间免费。

But the code I have given doesn't work for this problem ... Can anybody please give me the right query to check room availability properly ? 但是我提供的代码无法解决这个问题……有人可以给我正确的查询以正确检查房间的可用性吗?

I think your availability table isn't really availability, it's reservations. 我认为您的可用性表并不是真正的可用性,它是保留。 The lifecycle of the data in that table is unclear, but it seems like your query isn't picking up where there is no cin or cout. 该表中数据的生命周期尚不清楚,但似乎您的查询没有在没有cin或cout的地方进行。

$sql="select * from avalability where type = '$type' and cout>='$cin' and cin<='$cout'"

Should be something like 应该是这样的

$sql="SELECT * FROM avalability WHERE type = '$type' AND ((cout>='$cin' AND cin<='$cout'") OR (cout='' AND cin=''))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM