Home  >  Article  >  Backend Development  >  How to create a basic message board using PHP

How to create a basic message board using PHP

WBOY
WBOYOriginal
2023-09-24 10:41:011246browse

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.

  1. 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
    );

    The above code creates a database named message_board, which contains a table named messages for storing messages. The table structure includes the ID of the message record, user name, message content and creation time.

  2. 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>留言板</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>

    The 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.

  3. Processing submitted data
    Next, we create a post_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');
    ?>

    The 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 the header function.

  4. Display message content
    Finally, we create a display_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>';
    }
    ?>

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

Statement:
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