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
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
<?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.
$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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.