Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/d39eca494c55877793b143e91ceac3b2 to your computer and use it in GitHub Desktop.
Save code-boxx/d39eca494c55877793b143e91ceac3b2 to your computer and use it in GitHub Desktop.
PHP MYSQL Reservation System

PHP MYSQL RESERVATION SYSTEM

https://code-boxx.com/simple-php-reservation-system/

NOTES

  1. Create a database and import 1-reserve.sql.
  2. Change the database settings in 2-reserve.php to your own.
  3. Launch 3-reservation.php in the browser, and that’s it.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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;
<?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();
<!DOCTYPE html>
<html>
<head>
<title>DUMMY RESERVATION PAGE</title>
<meta charset="utf-8">
<link href="3-theme.css" rel="stylesheet">
</head>
<body>
<?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" min="<?=$mindate?>">
<label>Reservation Slot</label>
<select name="slot">
<option value="AM">AM</option>
<option value="PM">PM</option>
</select>
<input type="submit" value="Submit">
</form>
</body>
</html>
/* (A) SHARED */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
form, .note { max-width: 400px; }
.note {
margin-bottom: 20px;
padding: 10px;
border: 1px solid #ffb0b0;
background: #ffebeb;
}
form {
padding: 15px;
border: 1px solid #ededed;
background: #f5f5f5;
}
label, input, select {
display: block;
width: 100%;
}
label { margin: 10px 0; }
label:first-child { margin-top: 0; }
input, select {
border: 1px solid #dbdbdb;
padding: 10px;
}
input[type="submit"] {
background: #2a48b3;
color: #fff;
border: 0;
margin-top: 20px;
cursor: pointer;
}
<?php
// (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";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment