add: guestbook
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
*~
|
||||
*.~
|
||||
@@ -17,6 +17,20 @@ html {
|
||||
margin: auto;
|
||||
}
|
||||
|
||||
#guestbook_table {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
}
|
||||
|
||||
#guestbook_name {
|
||||
display: block;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
textarea {
|
||||
resize: none;
|
||||
}
|
||||
|
||||
/* unvisited link */
|
||||
a:link {
|
||||
color: #686bff;
|
||||
|
||||
@@ -15,5 +15,6 @@
|
||||
<a href="/pages/blog.html" aria-label="the blog"> the blog</a>
|
||||
<a href="//github.com/ALittlePatate" aria-label="github">github</a>
|
||||
<a href="/pgp/key.txt" aria-label="pgp key">pgp key</a>
|
||||
<a href="/php/guestbook.php" aria-label="guestbook">guestbook</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
51
php/guestbook.php
Normal file
51
php/guestbook.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<!--#include virtual="/pages/header.html" -->
|
||||
<?php
|
||||
$config = require "../../config.php";
|
||||
$conn = mysqli_connect($config['DB_ADDR'], $config['DB_USR'], $config['DB_PASSWD'], $config['DB_NAME']);
|
||||
|
||||
if (!$conn) {
|
||||
die("Connection failed: " . mysqli_connect_error());
|
||||
}
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
$name = $_POST['name'];
|
||||
$message = $_POST['message'];
|
||||
|
||||
$sql = "INSERT INTO guestbook (name, message) VALUES ('$name', '$message')";
|
||||
mysqli_query($conn, $sql);
|
||||
|
||||
header("Location: " . $_SERVER['PHP_SELF']);
|
||||
exit;
|
||||
} else {
|
||||
$name = "";
|
||||
$message = "";
|
||||
}
|
||||
|
||||
$sql = "SELECT * FROM guestbook";
|
||||
$result = mysqli_query($conn, $sql);
|
||||
|
||||
echo "<p>Welcome to my guestbook, be free to leave a message !</p>";
|
||||
echo "<br><br>";
|
||||
echo "<form action='" . $_SERVER['PHP_SELF'] . "' method='post'>";
|
||||
echo "<label for='name'>Name: </label>";
|
||||
echo "<input type='text' name='name' value='" . $name . "' required>";
|
||||
echo "<br><br>";
|
||||
echo "<label for='message'>Message: </label>";
|
||||
echo "<textarea name='message' required>" . $message . "</textarea><br><br>";
|
||||
echo "<input type='submit' value='Submit'>";
|
||||
echo "</form>";
|
||||
|
||||
echo "<br><br>";
|
||||
echo "<table id='guestbook_table' border='1'>";
|
||||
echo "<tr><th>Name</th><th>Message</th></tr>";
|
||||
while ($row = mysqli_fetch_assoc($result)) {
|
||||
echo "<tr>";
|
||||
echo "<td id='guestbook_name'>" . htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8') . "</td>";
|
||||
echo "<td>" . htmlspecialchars($row['message'], ENT_QUOTES, 'UTF-8') . "</td>";
|
||||
echo "</tr>";
|
||||
}
|
||||
echo "</table>";
|
||||
|
||||
mysqli_close($conn);
|
||||
?>
|
||||
<!--#include virtual="/pages/footer.html" -->
|
||||
8
sql/create_db.sql
Normal file
8
sql/create_db.sql
Normal file
@@ -0,0 +1,8 @@
|
||||
CREATE DATABASE guestbook;
|
||||
USE guestbook;
|
||||
|
||||
CREATE TABLE guestbook (
|
||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||
name VARCHAR(50),
|
||||
message TEXT
|
||||
);
|
||||
Reference in New Issue
Block a user