Home  >  Article  >  Backend Development  >  Analysis of the friend relationship management function of PHP social media application

Analysis of the friend relationship management function of PHP social media application

PHPz
PHPzOriginal
2023-08-13 09:52:441178browse

Analysis of the friend relationship management function of PHP social media application

Analysis of the friend relationship management function of PHP social media applications

Introduction:
In modern social media applications, friend relationship management is an important function. This feature enables users to add friends, browse friends lists, send messages, and more. In this article, we will use PHP to analyze how to implement the friend relationship management function.

1. Database design
First, we need to design a database to store user information and friend relationships. We can create two tables: users and friends.

  1. The users table contains basic information of users, such as user ID, user name, password, etc.
    Sample code:

    CREATE TABLE users (
     id INT(11) NOT NULL AUTO_INCREMENT,
     username VARCHAR(255) NOT NULL,
     password VARCHAR(255) NOT NULL,
     PRIMARY KEY (id)
    );
  2. The friends table is used to store friend relationships. It contains the IDs of the two users and the status of the friendship (e.g., pending, confirmed, etc.).
    Sample code:

    CREATE TABLE friends (
     user_id1 INT(11) NOT NULL,
     user_id2 INT(11) NOT NULL,
     status INT(1) DEFAULT 0,
     PRIMARY KEY (user_id1, user_id2),
     FOREIGN KEY (user_id1) REFERENCES users(id),
     FOREIGN KEY (user_id2) REFERENCES users(id)
    );

2. Add friend function
On the user interface, an option to add friends is provided. When the user clicks, we can do it through the following steps Implement the friend adding function:

  1. Verify whether the friend username entered by the user exists in the database.
    Sample code:

    // 在此省略数据库连接代码
    
    $friendUsername = $_POST['friend_username'];
    $friend = mysqli_fetch_assoc(mysqli_query("SELECT id FROM users WHERE username = '$friendUsername'"));
    
    if ($friend) {
     // 好友存在,可以添加好友
    } else {
     // 好友不存在,请重新输入
    }
  2. Insert the friend relationship into the friends table.
    Sample code:

    // 在此省略数据库连接代码
    
    $userID = $_SESSION['user_id'];
    $friendID = $friend['id']; // 通过上一步查询到的好友ID
    
    // 插入好友关系
    mysqli_query("INSERT INTO friends (user_id1, user_id2, status) VALUES ('$userID', '$friendID', 0)");
    
    if (mysqli_affected_rows() > 0) {
     // 好友添加成功
    } else {
     // 好友添加失败,请稍后重试
    }

3. Friends list function
Users can browse the friend list to view and manage their friends. We can use the following code to get the user's friend list:

Sample code:

// 在此省略数据库连接代码

$userID = $_SESSION['user_id'];

$friends = mysqli_query("SELECT users.username 
                         FROM friends 
                         JOIN users ON friends.user_id2 = users.id 
                         WHERE friends.user_id1 = '$userID' AND friends.status = 1");

while ($friend = mysqli_fetch_assoc($friends)) {
    echo $friend['username']."<br>";
}

4. Send message function
Users can send messages to friends, we can achieve this through the following code This function:

Sample code:

// 在此省略数据库连接代码

$userID = $_SESSION['user_id'];
$friendUsername = $_POST['friend_username'];
$message = $_POST['message'];

$friend = mysqli_fetch_assoc(mysqli_query("SELECT id FROM users WHERE username = '$friendUsername'"));

if ($friend) {
    $friendID = $friend['id'];
    // 发送消息
    mysqli_query("INSERT INTO messages (sender_id, receiver_id, message) 
                  VALUES ('$userID', '$friendID', '$message')");

    if (mysqli_affected_rows() > 0) {
        // 消息发送成功
    } else {
        // 消息发送失败,请稍后重试
    }
} else {
    // 好友不存在,请重新输入
}

Summary:
Through the above code examples, we can see how to use PHP to implement a simple friend relationship management function. Of course, there are more functional and safety considerations involved in practical applications. I hope this article can help readers better understand and implement the friend relationship management function of PHP social media applications.

The above is the detailed content of Analysis of the friend relationship management function of PHP social media application. 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