Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active July 21, 2023 13:54
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/8ded582613575bb75e0ddf45265a1a45 to your computer and use it in GitHub Desktop.
Save code-boxx/8ded582613575bb75e0ddf45265a1a45 to your computer and use it in GitHub Desktop.
PHP MYSQL Guestbook

PHP MYSQL GUESTBOOK

https://code-boxx.com/simple-php-guest-book/

IMAGES

talk

NOTES

  1. Create a database and import 1-database.sql.
  2. Change the database settings in 2-lib.php to your own.
  3. That's all, launch 3-page.php in your browser.

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 `guestbook` (
`post_id` bigint(20) NOT NULL,
`email` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`comment` text NOT NULL,
`datetime` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `guestbook`
ADD PRIMARY KEY (`post_id`,`email`),
ADD KEY `datetime` (`datetime`);
<?php
class GuestBook {
// (A) CONSTRUCTOR - CONNECT TO DATABASE
private $pdo;
private $stmt;
public $error;
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) GET GUEST BOOK ENTRIES
function get ($pid) {
$this->stmt = $this->pdo->prepare(
"SELECT * FROM `guestbook` WHERE `post_id`=? ORDER BY `datetime` DESC"
);
$this->stmt->execute([$pid]);
return $this->stmt->fetchall();
}
// (D) SAVE GUEST BOOK ENTRY
function save ($pid, $email, $name, $comment, $date=null) {
if ($date==null) { $date = date("Y-m-d H:i:s"); }
try {
$this->stmt = $this->pdo->prepare(
"REPLACE INTO `guestbook` (`post_id`, `email`, `name`, `comment`, `datetime`) VALUES (?,?,?,?,?)"
);
$this->stmt->execute([$pid, $email, $name, strip_tags($comment), $date]);
return true;
} catch (Exception $ex) {
$this->error = $ex->getMessage();
return false;
}
}
}
// (E) DATABASE SETTINGS - CHANGE TO YOUR OWN !
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "root");
define("DB_PASSWORD", "");
// (F) NEW GUEST BOOK OBJECT
$_GB = new GuestBook();
/* (A) WHOLE PAGE */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
body {
max-width: 600px;
padding: 20px;
margin: 0 auto;
background: #f5f5f5;
}
/* (B) SYSTEM MESSAGE */
div.note {
padding: 15px;
margin-bottom: 20px;
font-weight: 700;
background: #ffdddd;
}
/* (C) GUEST BOOK ENTRIES */
.gb-row {
display: flex;
align-items: center;
margin-bottom: 30px;
}
.gb-ico {
flex-shrink: 0;
width: 50px;
margin-right: 10px;
}
.gb-msg {
display: flex;
flex-wrap: wrap;
}
.gb-comment {
width: 100%;
margin-bottom: 3px;
font-size: 20px;
}
.gb-name, .gb-date { font-size: 14px; }
.gb-name {
font-weight: 700;
text-transform: uppercase;
color: #eb7e7e;
margin-right: 5px;
}
.gb-date {
flex-grow: 1;
color: #6a6a6a;
}
/* (D) SIGN GUESTBOOK */
#gb-form {
margin-top: 20px;
display: grid;
grid-template-areas: "a a a" "b c d";
grid-gap: 10px;
}
#gb-form * {
display: block;
width: 100%;
padding: 10px;
border: 0;
}
#gb-form textarea, #gb-form input[type=text], #gb-form input[type=email] {
border: 1px solid #e5e5e5;
}
#gb-form textarea {
grid-area: a;
height: 80px;
}
#gb-form input[type=text] { grid-area: b; }
#gb-form input[type=email] { grid-area: c; }
#gb-form input[type=submit] {
grid-area: d;
font-weight: 700;
color: #fff;
background: #cd3737;
cursor: pointer;
}
<!DOCTYPE html>
<html>
<head>
<title>DEMO GUEST BOOK PAGE</title>
<meta charset="utf-8">
<link rel="stylesheet" href="3-page.css">
</head>
<body>
<?php
// (A) PAGE INIT
// (A1) LOAD LIBRARY + SET PAGE ID
// GIVE EVERY PAGE A "UNIQUE ID"
// OR JUST USE "1" FOR A SINGLE GUESTBOOK FOR THE ENTIRE SITE
require "2-lib.php";
$pid = 1;
// (A2) SAVE GUEST BOOK ENTRY
if (isset($_POST["name"])) {
if ($_GB->save($pid, $_POST["email"], $_POST["name"], $_POST["comment"])) {
echo "<div class='note'>Guest book entry saved</div>";
} else {
echo "<div class='note'>$_GB->error</div>";
}
}
// (A3) GET GUEST BOOK ENTRIES
$entries = $_GB->get($pid); ?>
<!-- (B) GUEST BOOK ENTRIES -->
<div id="gb-entries">
<?php if (count($entries)>0) { foreach ($entries as $e) { ?>
<div class="gb-row">
<img class="gb-ico" src="talk.png">
<div class="gb-msg">
<div class="gb-comment"><?=$e["comment"]?></div>
<div class="gb-name"><?=$e["name"]?></div>
<div class="gb-date"> &#x2022; <?=$e["datetime"]?></div>
</div>
</div>
<?php }} ?>
</div>
<!-- (C) ADD NEW ENTRY -->
<form method="post" target="_self" id="gb-form">
<textarea name="comment" placeholder="Comment" required></textarea>
<input type="text" name="name" placeholder="Name" required>
<input type="email" name="email" placeholder="Email" required>
<input type="submit" value="Sign Guestbook">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment