Home > Article > Backend Development > The second-hand recycling website uses the message notification center function developed in PHP
The second-hand recycling website uses the message notification center function developed by PHP
With the rapid development of the Internet, second-hand recycling has become an increasingly common consumption method. In order to facilitate communication and information transfer between users, second-hand recycling websites usually need to provide a powerful message notification function. This article will introduce how to use PHP to develop an efficient message notification center function.
First, we need to create a database to store the user's message data. Assume that we have created a database named "notifications", which contains the following tables:
Next, we need to write PHP code to implement the message notification function. The first is the user registration function. We need to insert the user's information into the database when registering:
<?php // 连接数据库 $servername = "localhost"; $username = "root"; $password = ""; $dbname = "notifications"; $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 处理注册表单提交 if (isset($_POST['register'])) { $username = $_POST['username']; $email = $_POST['email']; // 将用户信息插入数据库 $sql = "INSERT INTO users (username, email) VALUES ('$username', '$email')"; $conn->query($sql); echo "注册成功!"; } $conn->close(); ?>
The next is the message sending function. When the user sends a message, we need to store the message in the database and send an email notification. Receiver:
<?php // 连接数据库 $servername = "localhost"; $username = "root"; $password = ""; $dbname = "notifications"; $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 处理发送消息表单提交 if (isset($_POST['send_message'])) { $sender_id = $_POST['sender_id']; $receiver_id = $_POST['receiver_id']; $message = $_POST['message']; // 将消息插入数据库 $sql = "INSERT INTO notifications (sender_id, receiver_id, message) VALUES ('$sender_id', '$receiver_id', '$message')"; $conn->query($sql); // 发送邮件通知接收者 $email_query = "SELECT email FROM users WHERE user_id = '$receiver_id'"; $email_result = $conn->query($email_query); $email = $email_result->fetch_assoc()['email']; mail($email, "您收到一条新的消息", $message); echo "消息发送成功!"; } $conn->close(); ?>
Finally, there is the message viewing function. Users can view and manage received messages in their own message center:
<?php // 连接数据库 $servername = "localhost"; $username = "root"; $password = ""; $dbname = "notifications"; $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 处理查看消息请求 if (isset($_GET['user_id'])) { $user_id = $_GET['user_id']; // 查询用户收到的未读消息 $unread_query = "SELECT * FROM notifications WHERE receiver_id = '$user_id' AND notification_id NOT IN (SELECT notification_id FROM read_notifications WHERE receiver_id = '$user_id')"; $unread_result = $conn->query($unread_query); echo "<h2>未读消息</h2>"; while ($row = $unread_result->fetch_assoc()) { echo "<p>".$row['message']."</p>"; } // 更新数据库,将未读消息标记为已读 $mark_read_query = "INSERT INTO read_notifications (notification_id, receiver_id) SELECT notification_id, receiver_id FROM notifications WHERE receiver_id = '$user_id' AND notification_id NOT IN (SELECT notification_id FROM read_notifications WHERE receiver_id = '$user_id')"; $conn->query($mark_read_query); // 查询用户所有消息 $all_notifications_query = "SELECT * FROM notifications WHERE receiver_id = '$user_id'"; $all_notifications_result = $conn->query($all_notifications_query); echo "<h2>所有消息</h2>"; while ($row = $all_notifications_result->fetch_assoc()) { echo "<p>".$row['message']."</p>"; } } $conn->close(); ?>
Through the above PHP code, we can implement a simple And powerful message notification center. Users can register, send messages, view and manage messages. In addition, we have also implemented real-time push of messages through email notifications. Second-hand recycling websites can be customized and expanded according to actual needs, making communication between users more convenient and efficient.
To sum up, using PHP to develop the message notification center function adds the ability to push real-time messages to the second-hand recycling website to facilitate communication and information transfer between users. This is of great significance to improve user experience and website activity. It is believed that in the near future, with the further development of the Internet, the message notification function will be applied and promoted in more websites and applications.
The above is the detailed content of The second-hand recycling website uses the message notification center function developed in PHP. For more information, please follow other related articles on the PHP Chinese website!