r/PHPhelp • • Oct 09 '17

Checking for availability between dates

So basically I am to make a reservation system for a resort. I have already made a database: https://imgur.com/f9aLyFZ. I really need to know how to check if dates are available when making a reservation.

So far I did this: $sql = "SELECT * FROM room_type_tbl

    INNER JOIN room_tbl ON room_type_tbl.room_type_id=room_tbl.room_type_id

    WHERE room_type_tbl.room_capacity='$roomcap' && ('$datein'>booking_tbl.checkin_date && '$dateout'<booking_tbl.checkoutdate)
";

if ($conn->query($sql) === TRUE) {
    //echo "Table MyGuests created successfully";
} else {
  echo "Error creating table: " . $conn->error;
}

It's not working and I'm about to go mad. Please help me.

8 Upvotes

7 comments sorted by

1

u/wh33t Oct 09 '17

Is room_id and room_number ever going to change? If not, copy over room_number to booking_tbl and it would be so easy. It would be a single query, with no joins.

If you want to maintain the table join you are attempting to use, please post the output and any errors you are receiving. Better yet, post a sample SQL dump so I can rebuild the structure and work on the queries myself.

1

u/MonsieurKyro Oct 09 '17

Here https://pastebin.com/F84GKSdW, i'm currently trying to output values on the table which I can't seem to do. (Please don't mind the other clutter, I'm only concerned about trying to filter out the entries that are available in between dates that the user entered). Here's my other php as well https://pastebin.com/Giw0JnZs . Thanks.

3

u/pablojohns Oct 09 '17 edited Oct 10 '17

Side note: please tell me you’re not storing the full card number and expiration in this database in plaintext. The fact that the field is for an integer is very concerning.

Please consider using a 3rd party payment processor as this is asking for MAJOR trouble, not just for the client but for your reputation as well. Cards should be run once through a processor for an authorization, and then kept securely on file WITH THE PROCESSOR, not in a database with the full card number, expiration, and customer billing info.

3

u/Dr_Midnight Oct 10 '17

While we're on the subject, increase the size of the email address column, and the password column. varchar(20) and varchar(30) are way too small for both columns respectively.

2

u/[deleted] Oct 10 '17

This

A good length of password hash is equal to the length of a nice hash. PHP has a nice hashing function built in for a while, so use that, and then let users put in a password of any length.

1

u/fz-09 Oct 10 '17

Are you trying to obtain all rooms with a given room capacity that are available on a given day?

SELECT r.* FROM room_tbl r
LEFT JOIN booking_tbl b
    ON b.table_id = r.table_id
    AND ? BETWEEN r.checkin_date AND r.checkout_date
JOIN room_type_tbl rt
    ON r.room_type_id = rt.room_type_id
WHERE rt.room_capacity = ?
    AND b.id IS NULL

1

u/dutchydownunder Oct 11 '17

^ Would need to check for start and end date the new guest wants to stay, not just a single date.

expected stay = es
already booked = ab

ab.endDate => es.startDate AND ab.startDate <= es.endDate

Then we left join room_tbl and booking_tbl on room_id on the above criteria and where booking_tbl.room_id is null

SELECT room_tbl.room_id
FROM `room_tbl` 'room'
LEFT JOIN `booking_tbl` 'booking'
    ON room.room_id = booking.room_id AND \es.startDate\ => booking.checkoutdate AND \es.endDate\ <= booking.checkin_date
WHERE booking.room_id is null

Should work, not sure, sitting in boring meeting browsing reddit ;-) Now write out all date range scenarios you can think of and start testing.

Some reservations I have about this: I would never use the actual rooms for bookings, rather have a free room count per type, and bookings per room type. This allows you to have a numerical buffer, for let's say taking rooms out of rotation for maintenance. Hotels normally assign the room number when you check in based on what is available. This also allows hotel staff to make crucial decisions, rather than a specific room being overbooked, a room type becomes overbooked instead. Personal preference perhaps.