+----+--------+----------+---------------+---------+
| id | userId | friendId | friendRequest | friends |
+----+--------+----------+---------------+---------+
| 1 | 1 | 3 | true | false |
+----+--------+----------+---------------+---------+
This is what I want. However, on page refresh, it seems that if you request ANYONE as a friend, it will somehow return every user as '$friendRequest' = true and '$friends' = false, despite the database not listing those users at all...
friends.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>";
}
}
request.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");
?>