Welcome to a quick tutorial on how to create a simple reservation system with PHP and MySQL. So you are offering a service and want to open up for online booking? Or maybe you have some rooms available for reservations?
A bare basic reservation system only has a few key components:
- A database table to store the reservations.
- A PHP library to process the reservations.
- The reservation page itself.
But just how is it done exactly? Let us walk an example in this guide – Read on!
TABLE OF CONTENTS
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
EXAMPLE CODE DOWNLOAD
Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
SORRY FOR THE ADS...
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.
Buy Me A Coffee Code Boxx eBooks
PHP RESERVATION SYSTEM
All right, let us now get into more detail about building a reservation system with PHP and MYSQL.
TUTORIAL VIDEO
STEP 1) RESERVATIONS DATABASE TABLE
CREATE TABLE `reservations` (
`res_id` bigint(20) NOT NULL,
`res_date` date,
`res_slot` varchar(32) DEFAULT NULL,
`res_name` varchar(255) NOT NULL,
`res_email` varchar(255) NOT NULL,
`res_tel` varchar(60) NOT NULL,
`res_notes` text DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `reservations`
ADD PRIMARY KEY (`res_id`),
ADD KEY `res_date` (`res_date`),
ADD KEY `res_slot` (`res_slot`),
ADD KEY `res_name` (`res_name`),
ADD KEY `res_email` (`res_email`),
ADD KEY `res_tel` (`res_tel`);
ALTER TABLE `reservations`
MODIFY `res_id` bigint(20) NOT NULL AUTO_INCREMENT;
Field | Description |
res_id |
Reservation ID. Primary key, auto-increment. |
res_date |
Self-explanatory, the reservation date. |
res_slot |
For time slot booking, this is an open text field that is up to you to decide – Offer hourly slots, AM/PM, or don’t use this at all (for whole day booking). |
res_name |
Name of the customer. |
res_email |
Email of the customer. |
res_tel |
Telephone number of the customer. |
res_notes |
Reservation notes, if any. |
That should cover all the basics, feel free to add more fields as your project requires.
STEP 2) PHP RESERVATION CLASS
<?php
class Reservation {
// (A) CONSTRUCTOR - CONNECT TO DATABASE
private $pdo; // pdo object
private $stmt; // sql statement
public $error; // error message
function __construct() {
$this->pdo = new PDO(
"mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=" . DB_CHARSET,
DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_NAMED
]);
}
// (B) DESTRUCTOR - CLOSE DATABASE CONNECTION
function __destruct() {
$this->pdo = null;
$this->stmt = null;
}
// (C) HELPER - EXECUTE SQL QUERY
function query ($sql, $data=null) : void {
$this->stmt = $this->pdo->prepare($sql);
$this->stmt->execute($data);
}
// (D) SAVE RESERVATION
function save ($date, $slot, $name, $email, $tel, $notes="") {
// (D1) CHECKS & RESTRICTIONS
// @TODO - ADD YOUR OWN RULES & REGULATIONS HERE
// MAX # OF RESERVATIONS ALLOWED?
// USER CAN ONLY BOOK X DAYS IN ADVANCE?
// USER CAN ONLY BOOK A MAX OF X SLOTS WITHIN Y DAYS?
// (D2) DATABASE ENTRY
$this->query(
"INSERT INTO `reservations` (`res_date`, `res_slot`, `res_name`, `res_email`, `res_tel`, `res_notes`) VALUES (?,?,?,?,?,?)",
[$date, $slot, $name, $email, $tel, $notes]
);
// (D3) EMAIL
// @TODO - REMOVE IF YOU WANT TO MANUALLY CALL TO CONFIRM OR SOMETHING
// OR EMAIL THIS TO A MANAGER OR SOMETHING
$subject = "Reservation Received";
$message = "Thank you, we have received your request and will process it shortly.";
@mail($email, $subject, $message);
return true;
}
// (E) GET RESERVATIONS FOR THE DAY
function getDay ($day="") {
// (E1) DEFAULT TO TODAY
if ($day=="") { $day = date("Y-m-d"); }
// (E2) GET ENTRIES
$this->query("SELECT * FROM `reservations` WHERE `res_date`=?", [$day]);
return $this->stmt->fetchAll();
}
}
// (F) DATABASE SETTINGS - CHANGE THESE TO YOUR OWN!
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "root");
define("DB_PASSWORD", "");
// (G) NEW RESERVATION OBJECT
$_RSV = new Reservation();
Yes, this library looks massive at first, but keep calm and look carefully.
- (A, B, G) When
$_RSV = new Reservation()
is created, the constructor will automatically connect to the database. The destructor closes the connection. - (C)
query()
A helper function to run an SQL query. - (D & E) There are only 2 functions here!
save()
This will be used to save the reservation when the user submits the reservation/booking form.get()
Gets all reservations for a specified day, an example of what you might want to use in your own admin panel.
- (F) Remember to change the database settings to your own.
That should cover all the raw essentials of a reservation system.
STEP 3) DUMMY RESERVATIONS PAGE
<?php
// (A) PROCESS RESERVATION
if (isset($_POST["date"])) {
require "2-reserve.php";
if ($_RSV->save(
$_POST["date"], $_POST["slot"], $_POST["name"],
$_POST["email"], $_POST["tel"], $_POST["notes"])) {
echo "<div class='note'>Reservation saved.</div>";
} else { echo "<div class='note'>".$_RSV->error."</div>"; }
}
?>
<!-- (B) RESERVATION FORM -->
<form id="resForm" method="post" target="_self">
<label>Name</label>
<input type="text" required name="name" value="Jon Doe">
<label>Email</label>
<input type="email" required name="email" value="jon@doe.com">
<label>Telephone Number</label>
<input type="text" required name="tel" value="123456789">
<label>Notes (if any)</label>
<input type="text" name="notes" value="Testing">
<?php
// @TODO - MINIMUM DATE (TODAY)
// $mindate = date("Y-m-d", strtotime("+2 days"));
$mindate = date("Y-m-d");
?>
<label>Reservation Date</label>
<input type="date" required name="date" value="<?=date("Y-m-d")?>">
<label>Reservation Slot</label>
<select name="slot">
<option value="AM">AM</option>
<option value="PM">PM</option>
</select>
<input type="submit" value="Submit">
</form>
No need to panic – This looks confusing but is actually very straightforward.
- (B) This should be self-explanatory, the HTML reservation form itself.
- (A) When the form is submitted, we use the library to process and save the reservation.
EXTRA) RESERVATIONS REPORT
// (A) GET ALL RESERVATIONS
require "2-reserve.php";
$all = $_RSV->getDay();
// (B) OUTPUT CSV
header("Content-Type: text/csv");
header("Content-Disposition: attachment;filename=reservations.csv");
if (count($all)==0) { echo "No reservations"; }
else {
// (B1) FIRST ROW - HEADERS
foreach ($all[0] as $k=>$v) { echo "$k,"; }
echo "\r\n";
// (B2) RESERVATION DETAILS
foreach ($all as $r) {
foreach ($r as $k=>$v) { echo "$v,"; }
echo "\r\n";
}
}
Everyone is going to have a different system and starting point, but here is another small script that will demonstrate how the library can be used to generate reports.
EXTRAS
That’s all for the project, and here are a few small extras that may be useful to you.
ADDING MORE FIELDS
- Change the database table, add more fields.
- Update
function save()
to also include the extra fields. - Lastly, update the HTML form.
I DON’T NEED THE TIME SLOT
Just leave it as NULL
, or remove it totally.
DATE RANGE BOOKING
This is kind of tricky, as everyone will have different requirements. For example:
- Some may have AM/PM timeslots. Booking an overnight stay may mean “Day 1 PM plus DAY 2 AM”, and not “2 full days”. So yep, that is 2 timeslots.
- Others may only offer “full-day bookings”. In this case, feel free to change the database to “date start and date end” instead of “date and time slot”.
- A few others may get extremely complicated with “pay by the hour”. In this case, it is best to store “time start” and “time end” instead.
There is no way to create a “one-size-fits-all” system here – This is your homework.
LINKS & REFERENCES
- User Registration Form In PHP MYSQL (With Email Verification) – Code Boxx
- Simple AJAX User Login Page (With PHP MySQL) – Code Boxx
- Simple PHP Admin Panel – Code Boxx
- Mail Not Sending Out? – Code Boxx
THE END
Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!
Omg, this is the best source code I’ve ever discovered, Thank you so much for taking the time and publish this for FREE. Bless you ❤
Greetings: W.S.TOH or Code Ninja, LoL. Thank you for taking the time to give us all this great tutoring – very grateful. I looked through tutorial and even people’s questions, and your comments and it helps a lot.
Here’s what I’m trying to setup. My son wants to make pizzas and donate them in 15 min intervals to less fortunate. Thus he wants people to be able to see what time slots are available for Tuesday his day off when he’ll be making pizzas and pick the 15 min slot. Should I treat each 15 min slot like a room unit? And can I display what’s available in a list (e.g. dropdown list) or any type they can pick from what’s available?
Yes, the reservation slot is an open field. Just change from AM/PM to something like 12:00, 12:15, 12:30, 12:45, etc…
Hi there,
i have updated everything it to godaddy server and my mail confirmation is not working, kindly help me with the following code.
thanks
https://code-boxx.com/fix-php-mail-not-working/
https://code-boxx.com/how-to-debug-php-code/
https://code-boxx.com/faq/#help
Check your mail settings and send logs. Sorry, I cannot offer free troubleshooting for your hosting service and mail servers – Hire a freelancer, or ask GoDaddy for support.
Thanks for the great tutorial! Is there a way to block slots when a reservation is already made for that time slot so that they become unavailable in the time selection field?
QUICK FIX) 2-reserve.php, section D1 – Use
getDay()
to check if specified day is already reserved.ADDITIONAL) 3-reservation.php, on date-time change, use AJAX to do a check.
Great tutorial, thanks for sharing! Something that I thought would be super complicated written out very simple. 🙂 I wonder if its absolutely necesaary to upload the .sql document, or its possible to creat the database manually in phpmyadmin. What does the code ALTER TABLE do?
ALTER – Change table structure, add keys and indexes.
https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
It’s up to you if you want to do it manually and learn along the way. PHPMyAdmin has an import SQL function anyway.
Thank you for your reply! 🙂