我有兩張桌子,房間和預訂。從ID未在其他查詢中的房間中選擇ID +(日期比較)
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
和我在我的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) ";
的結果是我把任何日期整個表。
任何人有任何的想法有什麼不好?
樣品
User input Checkin : 6-5-2017 Checkout : 8-5-2017 Adults : 1 Children : 0
我應該得到的房間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();
?>
你想要什麼?編輯您的問題並提供樣本數據和期望的結果。 –