-
Notifications
You must be signed in to change notification settings - Fork 1
/
addFriend.php
54 lines (47 loc) · 1.53 KB
/
addFriend.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
include 'includes/session.php';
include 'includes/dbConnect.php';
if (isset($_POST['add'])){
$target_email = mysqli_real_escape_string($connection, $_POST['target_email']);
$query = "SELECT * FROM users WHERE email='".$target_email."'";
$result = mysqli_query($connection, $query);
$numResult = mysqli_num_rows($result);
if ($numResult == 1){
$friend = mysqli_fetch_array($result);
$friend_id = $friend['user_id'];
$query = "SELECT * FROM friendships WHERE (user1_id=$user_id && user2_id=$friend_id) || (user1_id=$friend_id && user2_id=$user_id)";
$result = mysqli_query($connection, $query);
$numResult = mysqli_num_rows($result);
if ($numResult){
echo 'You are both already friends.';
header("Location: error.php");
exit;
}
else if ($user_id == $friend_id){
echo 'You can not add yourself as a friend.';
header("Location: error.php");
exit;
}
else {
if ($user_id < $friend_id){
$lower_id = $user_id;
$higher_id = $friend_id;
}
else {
$lower_id = $friend_id;
$higher_id = $user_id;
}
$query = "INSERT INTO friendships (user1_id, user2_id) VALUES ($lower_id, $higher_id)";
$result = mysqli_query($connection, $query);
$query = "INSERT INTO notifications (user_id, notification_message) VALUES ('$friend_id', '$name has added you as a friend.')";
$result = mysqli_query($connection, $query);
}
header("Location: dashboard.php");
}
else {
echo 'Not found';
header("Location: error.php");
exit;
}
}
?>