[英]Select ID from rooms where ID not in another query + (Date compare)
I have two tables, rooms and reservations. 我有两个桌子,房间和预订。
Rooms id description beds price 1 blah blah 2 100 2 blah blah 3 100 3 blah blah 4 100 4 blah blah 2 100 Reservations id room_id checkin checkout paid 1 1 5-5-2017 7-5-2017 1 2 2 5-5-2017 9-5-2017 1 3 2 10-5-2017 12-5-2017 0 4 3 2-5-2017 4-5-2017 1
and I have in my PHP code 而且我的PHP代码中有
user inputs $checkin, $checkout (dates) and $adults, $children
$sql = "SELECT id FROM rooms WHERE beds >= ($adults + $children) AND id NOT IN (SELECT room_id FROM reservations WHERE ($checkin BETWEEN checkin AND checkout) AND room_id IS NOT NULL) ";
The result is the whole table with any date i put in. 结果是整个表以及我输入的任何日期。
Anyone have any idea what is wrong? 任何人都知道哪里错了吗?
Samples 样品
User input Checkin : 6-5-2017 Checkout : 8-5-2017 Adults : 1 Children : 0
I should get room with id 3 and 4 我应该拿ID 3和4的房间
<?php
require('connect.php');
$checkin = $_POST['checkin'];
$checkout = $_POST['checkout'];
$adults = $_POST['adults'];
$children = $_POST['children'];
$valid = $_POST['cameFromRegisterPage'];
if(!isset($valid) || $valid != 'true'){
header("location: ./books.php");
}
$sql = "SELECT id FROM rooms WHERE beds >= ($adults + $children) AND id NOT IN (SELECT room_id FROM reservations WHERE ($checkin BETWEEN checkin AND checkout) AND room_id IS NOT NULL) ";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<div style='width:90%;background-color:#ffffff; padding:15px; text-align:left; margin-top:5px; margin-bottom:5px; opacity:0.9;'>";
echo "<p>Description : " .$row["description"]. "   |   Beds : " .$row["beds"]. "   |   Price per night : " .$row["ppn"]. "€   |   <a href='./rooms/room".$row['id'].".php' style='float:right;'>Book now</a></p>";
echo "</div>";
}
}
else {
echo "0 results";
//header("location: ./books.php");
}
$con->close();
?>
room_id is only in reservations table you shouldn't filter outside the NOT INT. room_id仅在预订表中,不应在NOT INT之外进行过滤。
$sql = "SELECT id
FROM rooms
WHERE beds >= ($adults + $children)
AND id NOT IN (SELECT room_id FROM reservations WHERE ('{$checkin}' >= checkin AND '{$checkin}' <= checkout) AND room_id is not null) ";
PS: I already worked into a hotel's software, just about business rules, it's important to separate adults from children beds on DB, in the future your system can experience some trouble. PS:我已经研究过酒店的软件,关于业务规则,在DB上将成年人和儿童分开放置是很重要的,将来您的系统可能会遇到一些麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.