Home >Backend Development >PHP Problem >Create a modifiable message board in php

Create a modifiable message board in php

PHPz
PHPzOriginal
2023-05-07 13:47:07766browse

With the gradual popularization of the Internet, websites have become one of the main ways for people to obtain information, communicate, and entertain. As one of the common communication tools on the website, the message board has become an indispensable part of the website. In this article, we will introduce how to use PHP to create a modifiable message board.

1. Environment setup

To create a message board, you first need to set up the corresponding environment. Here, we will use XAMPP to set up a local server environment.

The steps are as follows:

  1. Download and install the XAMPP software;
  2. Start the Apache and MySQL services of XAMPP;
  3. Open the browser and enter localhost , after selecting the language and authentication method on the page, enter the XAMPP management panel;
  4. In the management panel, click phpMyAdmin to enter the database management page.

2. Create database and data table

Create a database and name it guestbook. Create a data table named messages under the database. The data table contains the following fields:

Field name type length description
id int 11 Message ID, primary key, auto-increment
username varchar 50 Username
email varchar 100 Email
message text Message content
post_time datetime Message time

3. Create message board page

In this step, we will create A message board page that allows users to fill in messages and displays existing messages.

1. Make the UI of the message board page

Add the following code in the html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>可修改的留言板</title>
</head>
<body>
    <h1>留言板</h1>

    <div>
        <form method="POST" action="post_message.php">
            <h3>发布留言</h3>
            用户名:<input type="text" name="username"><br>
            邮箱:<input type="email" name="email"><br>
            留言:<br>
            <textarea name="message" rows="10" cols="50"></textarea><br>
            <input type="submit" value="发布">
        </form>
    </div>

    <div>
        <h3>最新留言</h3>
        <?php
            // 显示最新留言
        ?>
    </div>
</body>
</html>

As you can see, the message board page contains two parts, one part is for posting messages form, and the other part is a list of existing messages.

2. Submit a message

After submitting the form on the message board page, the submitted data needs to be saved in the database.

Create a file named post_message.php in the root directory to process the data in the message submission form.

Add the following code in the file:

<?php
// 连接数据库
$link = mysqli_connect("localhost", "root", "", "guestbook");
if (!$link) {
    die("数据库连接失败:" . mysqli_connect_error());
}

// 获取表单数据
$username = $_POST['username'];
$email = $_POST['email'];
$message = $_POST['message'];
$post_time = date("Y-m-d H:i:s");

// 插入留言到数据库中
$sql = "INSERT INTO messages (username, email, message, post_time)
        VALUES ('$username', '$email', '$message', '$post_time')";
$result = mysqli_query($link, $sql);
if ($result) {
    echo "留言发布成功!";
} else {
    echo "留言发布失败:" . mysqli_error($link);
}

// 关闭数据库连接
mysqli_close($link);
?>

In this file, first connect to the database and obtain the form data. The data is then inserted into the messages data table.

3. Display messages

On the message board page, the latest messages need to be displayed. To do this, you can display the list of messages from the database in the second part of the message board page.

In html, modify the following code:

<div>
    <h3>最新留言</h3>
    <?php
        // 查询留言信息
        $link = mysqli_connect("localhost", "root", "", "guestbook");
        if (!$link) {
            die("数据库连接失败:" . mysqli_connect_error());
        }
        $sql = "SELECT * FROM messages";
        $result = mysqli_query($link, $sql);
        mysqli_close($link);

        // 遍历留言数据,并显示到页面上
        while ($row = mysqli_fetch_assoc($result)) {
            ?>
            <hr>
            <div>
                <p>用户名:<?php echo $row['username']; ?></p>
                <p>邮箱:<?php echo $row['email']; ?></p>
                <p>时间:<?php echo $row['post_time']; ?></p>
                <p>留言内容:<?php echo $row['message']; ?></p>
            </div>
        <?php
        }
        ?>
</div>

In this code, first query the messages data table in the database, and save the queried message data to $result. Subsequently, the message data is traversed through a while loop and displayed on the page.

4. Modify messages

In the message board, users can modify the submitted messages.

In the html code, modify the code to display the message as follows:

<div>
    <h3>最新留言</h3>
    <?php
        // 查询留言信息
        $link = mysqli_connect("localhost", "root", "", "guestbook");
        if (!$link) {
            die("数据库连接失败:" . mysqli_connect_error());
        }
        $sql = "SELECT * FROM messages";
        $result = mysqli_query($link, $sql);
        mysqli_close($link);

        // 遍历留言数据,并显示到页面上
        while ($row = mysqli_fetch_assoc($result)) {
            ?>
            <hr>
            <div>
                <p>用户名:<?php echo $row['username']; ?></p>
                <p>邮箱:<?php echo $row['email']; ?></p>
                <p>时间:<?php echo $row['post_time']; ?></p>
                <p>留言内容:<?php echo $row['message']; ?></p>
                <a href="edit_message.php?id=<?php echo $row['id']; ?>">修改</a>
            </div>
        <?php
        }
        ?>
</div>

You can see that a hyperlink has been added for modifying the message.

In the edit_message.php file, add the following code:

<?php
// 连接数据库
$link = mysqli_connect("localhost", "root", "", "guestbook");
if (!$link) {
    die("数据库连接失败:" . mysqli_connect_error());
}

// 获取留言ID
$id = $_GET['id'];

// 根据ID查询留言信息
$sql = "SELECT * FROM messages WHERE id=$id LIMIT 1";
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_assoc($result);

// 显示留言内容的表单
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>修改留言</title>
</head>
<body>
    <h1>修改留言</h1>

    <div>
        <form method="POST" action="save_message.php">
            <h3>修改留言</h3>
            用户名:<input type="text" name="username" value="<?php echo $row['username']; ?>"><br>
            邮箱:<input type="email" name="email" value="<?php echo $row['email']; ?>"><br>
            留言:<br>
            <textarea name="message" rows="10" cols="50"><?php echo $row['message']; ?></textarea><br>
            <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
            <input type="submit" value="保存">
        </form>
    </div>
</body>
</html>

<?php
// 关闭数据库连接
mysqli_close($link);
?>

In this code, first query the message information based on the ID, and display the queried message data in the form. The user can then modify the message on this page. Among them, the hidden field input is used to save the message ID so that the database can be updated based on the ID when the message is saved later.

In the save_message.php file, add the following code:

<?php
// 连接数据库
$link = mysqli_connect("localhost", "root", "", "guestbook");
if (!$link) {
    die("数据库连接失败:" . mysqli_connect_error());
}

// 获取表单数据
$id = $_POST['id'];
$username = $_POST['username'];
$email = $_POST['email'];
$message = $_POST['message'];

// 更新留言
$sql = "UPDATE messages SET username='$username', email='$email', message='$message' WHERE id=$id";
$result = mysqli_query($link, $sql);
if ($result) {
    echo "留言更新成功!";
} else {
    echo "留言更新失败:" . mysqli_error($link);
}

// 关闭数据库连接
mysqli_close($link);
?>

In this file, first connect to the database and obtain the message data and message ID in the form. Subsequently, the message data in the database is updated according to the ID.

5. Summary

Through the introduction of this article, I believe readers have learned how to use PHP to create a modifiable message board. In the actual development process, the message board can also be expanded with richer functions, such as replying to messages, displaying the number of messages, sorting by time, etc.

The above is the detailed content of Create a modifiable message board in 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