How to create a basic message board using PHP

WBOY
Release: 2023-09-24 11:16:01
Original
1218 people have browsed it

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

    The above code creates a database namedmessage_board, which contains a table namedmessagesfor 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.

       留言板 
    

    留言板



    Copy after login

    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 thepost_message.phpfile.

  3. Processing submitted data
    Next, we create apost_message.phpfile to process the messages submitted by the user and save them to the database .

    prepare($sql); $statement->execute([$name, $message]); // 返回主页 header('Location: index.html'); ?>
    Copy after login

    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 themessagestable and redirect the user back to the homepage via theheaderfunction.

  4. Display message content
    Finally, we create adisplay_messages.phpfile to extract messages from the database and display them on the page.

    query($sql); $messages = $statement->fetchAll(PDO::FETCH_ASSOC); // 显示留言内容 foreach ($messages as $message) { echo '

    ' . $message['name'] . ': ' . $message['message'] . '

    '; } ?>
    Copy after login

    The above code first connects to the database, then queries themessagestable, and saves the results in the$messagesvariable. 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!

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