gpt4 book ai didi

PHP 好友请求系统无法正常工作

转载 作者:行者123 更新时间:2023-11-29 09:30:07 27 4
gpt4 key购买 nike

+----+--------+----------+---------------+---------+
| id | userId | friendId | friendRequest | friends |
+----+--------+----------+---------------+---------+
| 1 | 1 | 3 | true | false |
+----+--------+----------+---------------+---------+

这就是我想要的。然而,在页面刷新时,似乎如果您请求任何人作为 friend ,它会以某种方式将每个用户返回为 '$friendRequest' = true 和 '$friends' = false,尽管数据库根本没有列出这些用户......

friend .php

// Connect to USERS table
$userId = $_SESSION['id'];
$con = mysqli_connect("localhost", "root", "admin123", "master");
$sql = "SELECT * FROM users ORDER BY id DESC";
$result = mysqli_query($con, $sql);

// Connect to FRIENDS table
$sqli = $con->query("SELECT * FROM friends");
$data = $sqli->fetch_array();
$friendRequest = $data['friendRequest'];
$friends = $data['friends'];

// Build USERS table
while ($row = mysqli_fetch_array($result)) {
$profile = $row['profile'];
$id = $row['id'];
$firstName = $row['firstName'];
$lastName = $row['lastName'];
$city = $row['city'];
$state = $row['state'];
$bio = $row['bio'];

// Exclude user profile, current friends, and active friend requests

if ($id !== $userId && $friends !== "true" && $friendRequest !== "true") {

echo "
<div class='card m-0'>
<div class='text-center align-middle bg-white pt-3 pb-3'>
<img class='circle border mx-auto' style='width: 100px; height: 100px; object-fit: cover;' src='profile_pics/" . $profile . "'>
<h4 class='text-black font-weight-bolder'>" . $firstName . " " . $lastName . "</h4>
<div class='h7'><i class='fas fa-map-marker-alt text-danger'></i> " . $city . ", " . $state . "</div>
<form action='friends.php' method='post'>
<a href='request.php?id=" . $id . "' class='btn btn-primary mx-auto mt-3'>Send Friend Request</a>
</form>
</div>
</div>";

} else {
echo "Friend request: " . $friendRequest . "<br>Friends? " . $friends . "<br>";
}
}

请求.php

<?php
require "functions.php";
require "logincheck.php";

$userId = $_SESSION['id'];
$friendId = $_GET['id'];
$con = mysqli_connect("localhost", "root", "", "master");
$sqli = ("INSERT INTO friends (userId, friendId, friendRequest, friends) VALUES ('$userId', '$friendId', 'true', 'false')");
mysqli_query($con, $sqli);

header("Location: friends.php");

?>

最佳答案

在获取 friends 表的第一行并设置变量 $friendRequest$friends 后,它们表示用户 1 之间的关系和用户 3。当您开始循环所有用户时,您永远不会更改这些值,因此即使您检查例如,它们仍然代表这些用户之间的关系。用户2。

我想您想要的是选择特定用户的所有(待处理)好友请求(SELECT * FROM Friends WHERE userId = ? - 确保使用准备好的语句!),然后在循环中检查当前循环的用户是否出现在 friends 查询的结果中。如果是,您已经向他发送了好友请求。

关于PHP 好友请求系统无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58903083/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com