A guestbook is an essential interactive feature for many websites, allowing users to leave feedback, suggestions, or comments. In this article, we will explain how to develop a simple yet effective guestbook using PHP, enabling users to submit and view messages on a webpage.
First, we need to create a database to store the guestbook messages. You can use phpMyAdmin or any other MySQL management tool to create the database. Below is the SQL query for creating the database and table:
CREATE TABLE messages ( id INT(11) AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, email VARCHAR(50) NOT NULL, message TEXT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
Next, we need to create a form where users can enter their name, email, and message, then submit the data to the server. Below is the HTML code for the form:
<form action="submit.php" method="post"> <label for="name">Name:</label> <input type="text" name="name" id="name" required><br> <label for="email">Email:</label> <input type="email" name="email" id="email" required><br> <label for="message">Message:</label> <textarea name="message" id="message" cols="30" rows="10" required></textarea><br> <input type="submit" value="Submit Message"> </form>
After the user submits the form, we need to process the data with PHP and save it to the database. Below is a simple PHP script to handle the form submission and save the message to the database:
<?php // Connect to the database $mysqli = new mysqli('localhost', 'username', 'password', 'guestbook'); // Check the connection if ($mysqli->connect_error) { die('Database connection failed: ' . $mysqli->connect_error); } // Process form data $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; // Prepare SQL query to insert message $sql = "INSERT INTO messages (name, email, message) VALUES ('$name', '$email', '$message')"; // Execute the query if ($mysqli->query($sql) === true) { echo 'Message submitted successfully!'; } else { echo 'Message submission failed: ' . $mysqli->error; } // Close the database connection $mysqli->close(); ?>
Finally, we need to retrieve and display the messages from the database on the webpage. Below is the PHP code to fetch and display the messages:
<?php // Connect to the database $mysqli = new mysqli('localhost', 'username', 'password', 'guestbook'); // Check the connection if ($mysqli->connect_error) { die('Database connection failed: ' . $mysqli->connect_error); } // Query the messages $sql = "SELECT * FROM messages ORDER BY created_at DESC"; $result = $mysqli->query($sql); // Display the messages if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo '<div>'; echo '<h3>' . $row['name'] . ' (' . $row['email'] . ')</h3>'; echo '<p>' . $row['message'] . '</p>'; echo '</div>'; } } else { echo 'No messages yet'; } // Close the database connection $mysqli->close(); ?>
With the steps above, we have successfully developed a simple guestbook feature. Users can submit messages through the form, and the messages are stored in the database and displayed on the webpage. Thanks to the simplicity and power of PHP, we can quickly develop such interactive features. We hope this article has been helpful to you!