The second-hand recycling website developed in PHP realizes the function of replying to messages and consultations
With the progress of society and the enhancement of people's awareness of environmental protection, the second-hand recycling industry is gradually emerging. In order to better meet the needs of users, many second-hand recycling websites have begun to add message consultation and reply functions to help users solve problems and provide more accurate services. This article will introduce how to use PHP to develop a second-hand recycling website and implement the message consultation reply function.
First, we need to set up a PHP development environment to ensure that we can run PHP files. Below is a simple PHP code example for receiving user comments and saving them to the database.
<?php // 连接数据库 $conn = mysqli_connect("localhost", "username", "password", "database"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); exit(); } // 接收用户留言 if (isset($_POST['message'])) { $message = $_POST['message']; // 将留言插入数据库 $sql = "INSERT INTO messages (message) VALUES ('$message')"; if (mysqli_query($conn, $sql)) { echo "留言成功"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } } // 关闭数据库连接 mysqli_close($conn); ?>
The above code first connects to the MySQL database and uses the mysqli_connect function to pass the host, username, password and database information. Then, by determining whether the content of $_POST['message'] is received, it is determined whether the user has submitted a message. If there is message content, insert it into the database.
Next, we need to implement the message consultation reply function. First, we need to display the message list and provide a reply button for the administrator to reply to the message. Below is a simple PHP code example for displaying the message list and replying to messages.
<?php // 连接数据库 $conn = mysqli_connect("localhost", "username", "password", "database"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); exit(); } // 查询留言列表 $sql = "SELECT * FROM messages"; $result = mysqli_query($conn, $sql); // 输出留言和回复表单 while ($row = mysqli_fetch_assoc($result)) { echo "留言:" . $row['message'] . "<br>"; echo "回复:<br>"; echo "<form action='reply.php' method='post'>"; echo "<input type='hidden' name='message_id' value='" . $row['id'] . "'>"; echo "<textarea name='reply'></textarea><br>"; echo "<input type='submit' value='回复'>"; echo "</form>"; echo "<hr>"; } // 关闭数据库连接 mysqli_close($conn); ?>
The above code first connects to the database and queries all data in the messages table. Then, use the mysqli_fetch_assoc function to obtain the query results line by line, and output the message content to the page. At the same time, a reply form is generated for each message, which contains a hidden message ID used to identify the message corresponding to the reply. Finally, display a reply button for administrators to reply to messages.
Next, we need to write the code for the reply function. In the reply.php file, we can get the message ID corresponding to the reply through $_POST['message_id'] and insert the reply content into the database. Below is a simple PHP code example.
<?php // 连接数据库 $conn = mysqli_connect("localhost", "username", "password", "database"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); exit(); } // 接收回复内容和留言ID $reply = $_POST['reply']; $message_id = $_POST['message_id']; // 将回复内容插入数据库 $sql = "UPDATE messages SET reply='$reply' WHERE id='$message_id'"; if (mysqli_query($conn, $sql)) { echo "回复成功"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } // 关闭数据库连接 mysqli_close($conn); ?>
The above code obtains the reply content through $_POST['reply'], and obtains the message ID corresponding to the reply through $_POST['message_id']. Then, update the reply content to the message table messages.
Through the above code example, we can implement a simple second-hand recycling website and add a message consultation reply function to help users solve problems and provide more accurate services. Of course, according to actual needs, we can also design more complex functions and interfaces for the website. I hope this article will be helpful to you in your second-hand recycling website development.
The above is the detailed content of Second-hand recycling website developed with PHP to implement message consultation and reply function. For more information, please follow other related articles on the PHP Chinese website!