Home >Backend Development >PHP Tutorial >Second-hand recycling website uses consultation and message function developed in PHP

Second-hand recycling website uses consultation and message function developed in PHP

WBOY
WBOYOriginal
2023-07-01 14:07:391310browse

The second-hand recycling website uses the consultation message function developed by PHP

With the rise of the second-hand commodity market, more and more people are paying attention to second-hand recycling websites. In order to provide a better user experience, many second-hand recycling websites have added consultation and message functions to facilitate users to communicate with the platform. This article will introduce a consultation message function developed using PHP and provide corresponding code examples.

Before developing the consultation message function, you must first create a database table to store consultation information. The following is a simple database table structure example:

CREATE TABLE `messages` (
   `id` INT(11) NOT NULL AUTO_INCREMENT,
   `name` VARCHAR(50) NOT NULL,
   `email` VARCHAR(50) NOT NULL,
   `message` TEXT NOT NULL,
   `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
   PRIMARY KEY (`id`)
);

Next, we can start writing PHP code to implement the consultation message function. The first is the front-end part, which is used to display the message form and message list. The following is a simple mixed example of HTML and PHP:

<!DOCTYPE html>
<html>
<head>
   <title>咨询留言</title>
</head>
<body>
   <h1>咨询留言</h1>

   <?php
   // 处理表单提交
   if($_SERVER['REQUEST_METHOD'] == 'POST'){
      // 获取表单数据
      $name = $_POST['name'];
      $email = $_POST['email'];
      $message = $_POST['message'];

      // 将留言存入数据库
      $conn = new mysqli('localhost', 'username', 'password', 'database');
      $sql = "INSERT INTO messages (name, email, message) VALUES ('$name', '$email', '$message')";
      $result = $conn->query($sql);

      if($result){
         echo "留言已成功提交!";
      }else{
         echo "留言提交失败,请稍后再试。";
      }
   }
   ?>

   <h2>留言表单</h2>
   <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
      <label for="name">姓名:</label>
      <input type="text" id="name" name="name" required><br><br>
      <label for="email">邮箱:</label>
      <input type="email" id="email" name="email" required><br><br>
      <label for="message">留言:</label><br>
      <textarea id="message" name="message" rows="4" cols="50" required></textarea><br><br>
      <input type="submit" value="提交留言">
   </form>

   <hr>

   <h2>留言列表</h2>
   <?php
   // 从数据库获取留言列表
   $conn = new mysqli('localhost', 'username', 'password', 'database');
   $sql = "SELECT * FROM messages ORDER BY created_at DESC";
   $result = $conn->query($sql);

   if($result->num_rows > 0){
      while($row = $result->fetch_assoc()){
         echo "<p>姓名:" . $row['name'] . "</p>";
         echo "<p>邮箱:" . $row['email'] . "</p>";
         echo "<p>留言时间:" . $row['created_at'] . "</p>";
         echo "<p>留言内容:" . $row['message'] . "</p>";
         echo "<hr>";
      }
   }else{
      echo "暂无留言。";
   }
   ?>
</body>
</html>

The above code snippet demonstrates the implementation of the consultation message function. Users can fill in their name, email address and message content in the form, and click the submit button. After submission, the form data will be inserted into the database and displayed in the message list.

It should be noted that the database connection information in the above code needs to be modified according to the actual situation. In addition, in order to ensure security, it is recommended to encrypt sensitive information in the database connection part to avoid leakage.

To sum up, using PHP to develop the consultation and message function of a second-hand recycling website can increase the interaction between users and the platform. Through simple front-end forms and database storage, users can easily leave messages and view the message list at any time. The code examples provided above can be used as a reference, and developers can modify and extend them according to actual needs to meet different application scenarios.

The above is the detailed content of Second-hand recycling website uses consultation and message function developed 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