Dit zijn de scriptjes om gegevens weg te schrijven naar een database:
2-reserve.php
<?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=**knip**" . DB_HOST . "dbname=**knip**" . DB_NAME . "charset=utf8mb4" . DB_CHARSET . "dbuser=**knip**" . DB_USER . "dbpassword=**knip**" . DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_NAMED
]);
}
class DatabaseConnection {
public function __construct() {
try {
return new PDO('mysqli:host=xxx;dbname=xxx', 'root', '');
} catch(PDOException $e) {
exit('Database error');
}
}
}
// (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", "***************");
define("DB_NAME", "*************");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "********");
define("DB_PASSWORD", "*********");
// (G) NEW RESERVATION OBJECT
$_RSV = new Reservation();
?>
tweede script: 3-reservation-php
[code]
<!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="[email protected]">
<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>
Derde script: 4-report.php
<?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";
}
}
?>
[code]