Home >Backend Development >PHP Problem >How to implement the add friend function in php
In recent years, social networks have become an indispensable part of people's daily lives. At the same time, friend relationships, as an important part of social networks, have also received more and more attention in their implementation. This article will introduce how to use PHP to implement the function of adding friends.
1. Database design
In the process of implementing the friend adding function, a friend relationship table needs to be designed. The table should contain at least the following fields:
2. Add friends
When adding friends in PHP, you need to go through the following steps:
The following is a simple PHP code example to implement the function of adding friends:
<?php // 连接数据库 $con = mysqli_connect("localhost","root","mypassword","mydatabase"); // 获取用户输入 $user_id1 = $_POST['user_id1']; $user_id2 = $_POST['user_id2']; // 验证用户输入是否合法 if(!is_numeric($user_id1) || !is_numeric($user_id2)) { echo "Error: 用户ID必须为数字"; exit(); } // 检查要添加的好友是否已经是用户的好友 $sql = "SELECT * FROM friend_relation WHERE ((user_id1 = $user_id1 AND user_id2 = $user_id2) OR (user_id2 = $user_id1 AND user_id1 = $user_id2)) AND status = 1"; $result = mysqli_query($con,$sql); if(mysqli_num_rows($result) > 0) { echo "该用户已经是您的好友!"; exit(); } // 如果两个用户之间没有好友关系,则添加一条新的好友关系记录,其中状态为“0”(待处理) $sql = "INSERT INTO friend_relation (user_id1,user_id2,status) VALUES ($user_id1,$user_id2,0)"; if(mysqli_query($con,$sql)) { echo "添加好友请求已经发送!"; } else { echo "Error: " . mysqli_error($con); } mysqli_close($con); ?>
In the above code, we use the mysqli extension to connect to the MySQL database and pass The POST method obtains the IDs of the two users, and then performs input verification and friend relationship checking. If the friend to be added is already a friend of the user, a prompt message is output; if there is no friend relationship between the two users, a new friend relationship record is added to the database, and a success prompt message is output.
3. Friend request processing
In the friend adding function, it is also necessary to implement the processing of friend requests. When a user sends a friend request to another user, the requested user needs to perform corresponding processing, including accepting or rejecting the request.
In the database design, we set the friend relationship status (status) to "0" to indicate that the friend request has not been processed, to "1" to indicate that the friend is already a friend, and to "-1" to indicate that the friend is The request has been denied. Therefore, in the friend request processing function, we need to add the corresponding status update code.
The following is a simple PHP code example for processing friend requests:
<?php // 连接数据库 $con = mysqli_connect("localhost","root","mypassword","mydatabase"); // 获取用户输入 $user_id1 = $_POST['user_id1']; $user_id2 = $_POST['user_id2']; $action = $_POST['action']; // 操作类型,可选值包括“accept”和“reject” // 验证用户输入是否合法 if(!is_numeric($user_id1) || !is_numeric($user_id2)) { echo "Error: 用户ID必须为数字"; exit(); } // 更新好友关系状态 if($action == "accept") { $sql = "UPDATE friend_relation SET status = 1 WHERE user_id1 = $user_id1 AND user_id2 = $user_id2"; if(mysqli_query($con,$sql)) { echo "您已经同意该好友请求!"; } else { echo "Error: " . mysqli_error($con); } } else if($action == "reject") { $sql = "UPDATE friend_relation SET status = -1 WHERE user_id1 = $user_id1 AND user_id2 = $user_id2"; if(mysqli_query($con,$sql)) { echo "您已经拒绝了该好友请求!"; } else { echo "Error: " . mysqli_error($con); } } else { echo "Error: 请输入正确的操作类型!"; } mysqli_close($con); ?>
In the above code, we obtained the IDs and operation types of two users through the POST method, "Accept" means accepting the friend request, and "reject" means rejecting the friend request. Then, we updated the friend relationship status through the UPDATE statement and output the corresponding prompt information.
Through the above PHP code example, we can easily implement the functions of adding friends and processing friend requests. In actual development, it can be appropriately modified according to specific needs and optimized in terms of security and performance.
The above is the detailed content of How to implement the add friend function in php. For more information, please follow other related articles on the PHP Chinese website!