Home > Article > Backend Development > How to use PHP to develop guest message board function
How to use PHP to develop guest message board function
Introduction:
Guest message board is one of the common functions in website development, which allows users to Leave a comment or message. In this article, we will use PHP language to develop a simple guest message board function that allows users to leave messages on the web page and display all messages. Below are specific steps and code examples.
Step 1: Create database and data table
First, we need to create a database to store the message information in the message board. Open phpMyAdmin (or other MySQL management tools) and create a database named "guestbook". Then, create a data table named "messages" in the database, which will contain information about messages. The table structure is as follows:
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
);
Step 2: Create a message board form
Next, we need to create a form on the web page , allowing users to enter their name, email, and message content and submit it to the server. Here is a simple form example:
Steps Three: Processing message data
When the user submits a message, we need to use PHP code to process the message data and save it to the database. Create a file named "submit.php" and use the following code to process the form data:
// Connect to the database
$mysqli = new mysqli('localhost' , 'username', 'password', 'guestbook');
// Check connection
if ($mysqli->connect_error) {
die('数据库连接失败:' . $mysqli->connect_error);
}
// Process form data
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Prepare the SQL statement to insert messages
$sql = "INSERT INTO messages (name, email, message) VALUES ('$name', '$email', '$message')";
//Perform insert operation
if ($mysqli->query($sql) === true) {
echo '留言提交成功!';
} else {
echo '留言提交失败:' . $mysqli->error;
}
// Close the database connection
$mysqli->close();
?>
Step 4: Display the message list
Finally, we need to write PHP code to Retrieve message data from the database and display the message list on the web page. The following is a simple code example:
//Connect to the database
$mysqli = new mysqli('localhost', 'username', 'password', 'guestbook') ;
// Check the connection
if ($mysqli->connect_error) {
die('数据库连接失败:' . $mysqli->connect_error);
}
// Query the message data
$sql = " SELECT * FROM messages ORDER BY created_at DESC";
$result = $mysqli->query($sql);
// Display message list
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 '暂无留言';
}
// Close the database connection
$mysqli->close();
?> ;
Conclusion:
Through the above steps, we successfully developed a simple guest message board function. Users can leave messages on the web page, the message data will be saved in the database, and all messages will be displayed on the web page. Taking advantage of the PHP language, we can quickly and simply develop a powerful guest message board. Hope this article is helpful to you!
The above is the detailed content of How to use PHP to develop guest message board function. For more information, please follow other related articles on the PHP Chinese website!