2017-05-29 60 views
0

我有兩張桌子,房間和預訂。從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"]. " &emsp; | &ensp; Beds : " .$row["beds"]. " &emsp; | &ensp; Price per night : " .$row["ppn"]. "€ &emsp; | &ensp; <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(); 
?> 
+0

你想要什麼?編輯您的問題並提供樣本數據和期望的結果。 –

回答

1

ROOM_ID僅在預訂表,你不應該在不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:我已經工作到酒店的軟件,只是業務規則,它在DB分開兒童牀大人,您在未來的系統能夠遇到一些麻煩是非常重要的。

+0

對不起,我沒有複製粘貼我的數據庫名稱我寫在這裏,我犯了一個錯誤,這是在我的數據庫中是正確的。 – Stelios

+0

@Stelios沒關係,我編輯了 – calexandre

+0

感謝您的提示,但這只是一個學校項目,所以沒什麼大不了的! – Stelios