How to create a basic message board using PHP
How to create a basic message board using PHP
The message board is a common website feature that allows users to leave their comments and opinions on the web page. In this article, we will introduce how to create a basic message board using PHP language. Below are specific code examples.
-
Create database
First, we need to create a database to store user messages. MySQL or other relational databases can be used. In this example, we use MySQL.CREATE DATABASE message_board; USE message_board; CREATE TABLE messages ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, message TEXT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
Copy after loginThe above code creates a database named
message_board
, which contains a table namedmessages
for storing messages. The table structure includes the ID of the message record, user name, message content and creation time. Create the front-end interface
Next, we create a simple front-end web page, let the user fill in the user name and message content, and submit it to the server.<!DOCTYPE html> <html> <head> <title>留言板</title> </head> <body> <h1 id="留言板">留言板</h1> <form method="POST" action="post_message.php"> <label>用户名:</label> <input type="text" name="name" required><br> <label>留言:</label> <textarea name="message" required></textarea><br> <input type="submit" value="提交"> </form> </body> </html>
Copy after loginThe above code creates an HTML form that contains two fields: user name and message content. When the user clicks the submit button, the form data will be sent to the
post_message.php
file.Processing submitted data
Next, we create apost_message.php
file to process the messages submitted by the user and save them to the database .<?php // 连接数据库 $host = 'localhost'; $db = 'message_board'; $user = 'root'; $password = ''; $pdo = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $password); // 获取用户提交的数据 $name = $_POST['name']; $message = $_POST['message']; // 插入数据到数据库 $sql = "INSERT INTO messages (name, message) VALUES (?, ?)"; $statement = $pdo->prepare($sql); $statement->execute([$name, $message]); // 返回主页 header('Location: index.html'); ?>
Copy after loginThe above code first connects to the MySQL database, and then obtains the user name and message content submitted by the user. Next, it will insert this data into the
messages
table and redirect the user back to the homepage via theheader
function.Display message content
Finally, we create adisplay_messages.php
file to extract messages from the database and display them on the page.<?php // 连接数据库 $host = 'localhost'; $db = 'message_board'; $user = 'root'; $password = ''; $pdo = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $password); // 从数据库中获取留言内容 $sql = 'SELECT * FROM messages ORDER BY created_at DESC'; $statement = $pdo->query($sql); $messages = $statement->fetchAll(PDO::FETCH_ASSOC); // 显示留言内容 foreach ($messages as $message) { echo '<p><strong>' . $message['name'] . ':</strong> ' . $message['message'] . '</p>'; } ?>
Copy after loginThe above code first connects to the database, then queries the
messages
table, and saves the results in the$messages
variable. Then, it uses a loop to display the content of each message on the page.
At this point, we have completed the creation of a basic message board. Users can fill in messages on the front-end page, and the background will store the message content in the database and display it on the home page. This is just a simple example, you can modify and extend it according to your actual needs. I hope this article can help you get started with PHP development and create your own message board function.
The above is the detailed content of How to create a basic message board using PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
