$pgjs = [];
head($pgtitle, $pgdescription, $pgcss, $pgjs);
?>
-<body background="/image/background/bluesky.jpg" style="color: #fcff36;">
+<body background="/image/background/bluesky.jpg">
<center>
<table>
<tr>
<td>
- <center><img src="/image/gif/guestbook.gif"></center>
+ <center><img src="/image/gif/guestbook.gif" width="200"></center>
<?php
+ error_reporting(E_ALL);
+ ini_set("display_errors", 1);
+
+ // database config.
+ $host = "localhost";
+ $dbname = "guestbook";
+ $username = "";
+ $password = "";
+
+ // connect to database
+ $conn = new mysqli($host, $username, $password, $dbname);
+
+ // check connection
+ if($conn->connect_error){
+ die("Connection failed: {$conn->connect_error}");
+ }
+
+ // entry submission
if ($_SERVER["REQUEST_METHOD"] == "POST"){
- $name = $POST['name'];
- $website = $POST['website'];
- $email = $POST['email'];
- $comment = $POST['comment'];
+ $name = $_POST['name'];
+ $country = $_POST['country'];
+ $website = $_POST['website'];
+ $email = $_POST['email'];
+ $comment = $_POST['comment'];
+ $ip_addr = $_SERVER['REMOTE_ADDR'];
+
+ // query
+ $sql = "INSERT INTO entries (name, country, website, email, comment, ip_addr)
+ VALUES ('$name', '$country', '$website', '$email', '$comment', '$ip_addr');";
+ $conn->query($sql); // submit query
+
+ header("Location: " . $_SERVER["PHP_SELF"]);
+ die();
};
+
+ $result = $conn->query("SELECT name, country, website, email, comment, written_at
+ FROM entries
+ WHERE status='visible'
+ ORDER BY written_at DESC;");
?>
- <form method="POST" action="<?= htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
- <label for"name">Name: </label><input type="text" name="name" required>
- <br>
- <label for"website">My website: </label><input type="text" name="website" placeholder="optional">
- <br>
- <label for"email">My e-mail: </label><input type="email" name="email" placeholder="optional">
- <br>
- <label for="comment">Comment: </label><br><textarea rows="10" cols="35" required></textarea>
- <br>
- <button type="submit">Sign in the guestbook</button>
- </form>
+ <center>
+ <form method="POST" action="<?= htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
+ <label for="name">Name: </label><input type="text" name="name" required>
+ <br>
+ <label for="country">Country: </label><input type="text" name="country" placeholder="optional">
+ <br>
+ <label for="website">My website: </label><input type="text" name="website" placeholder="optional">
+ <br>
+ <label for="email">My e-mail: </label><input type="email" name="email" placeholder="only me can see">
+ <br>
+ <label for="comment">Comment: </label><br><textarea name="comment" rows="10" cols="35" required></textarea>
+ <br>
+ <button type="submit">Sign the guestbook</button>
+ </form>
+ </center>
+
+ <br><br>
+
+ <center><h2>Entries</h2></center>
+ <table border="1" cellspacing="" width="600">
+ <tr>
+ <th>Name</th>
+ <th>Country</th>
+ <th>Web site</th>
+ <th>Comment</th>
+ </tr>
+ <?php
+ if($result && $result->num_rows > 0){
+ while($row = $result->fetch_assoc()){
+ echo "<tr>";
+ echo "<td>" . htmlspecialchars($row['name']) . "</td>";
+ echo "<td>" . htmlspecialchars($row['country']) . "</td>";
+ if(!empty($row['website'])){
+ echo "<td><a href='" . htmlspecialchars($row['website']) . "' target='_blank'>" . htmlspecialchars($row['website']) . "</a></td>";
+ } else{
+ echo "<td></td>";
+ }
+ echo "<td>" . htmlspecialchars($row['comment']) . "</td>";
+ echo "</tr>";
+ }
+ }
+ ?>
+ </table>
<br><br>
<center><a href="/"><img src="/image/navigation/backhomepage.gif"></a></center>