How to use PHP to develop guest message board function

PHPz
Release: 2023-08-26 17:38:02
Original
623 people have browsed it

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
Copy after login

);

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:

<label for="name">姓名:</label>
<input type="text" name="name" id="name" required><br>

<label for="email">电子邮件:</label>
<input type="email" name="email" id="email" required><br>

<label for="message">留言内容:</label>
<textarea name="message" id="message" cols="30" rows="10" required></textarea><br>

<input type="submit" value="提交留言">
Copy after login

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);
Copy after login
Copy after login

}

// 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 '留言提交成功!';
Copy after login

} else {

echo '留言提交失败:' . $mysqli->error;
Copy after login

}

// 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);
Copy after login
Copy after login

}

// 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>';
}
Copy after login

} else {

echo '暂无留言';
Copy after login

}

// 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!