-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendFriendRequest.php
88 lines (71 loc) · 2.69 KB
/
sendFriendRequest.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
session_start();
include_once 'config.php';
$toWrite = 'nothing';
$user_id;
$user_email;
$inboxFull;
if(!isset($_SESSION['userEmail'])){
header("location: index.php?error=loginFirst");
}
if(isset($_SESSION["user_id"])){
$user_id = $_SESSION["user_id"];
}
if(isset($_SESSION["userEmail"])){
$user_email = $_SESSION["userEmail"];
}
if(isset($_POST['friendEmail'])){
$friendEmail = $_POST['friendEmail'];
if($friendEmail == $user_email){
header("location: profile.php?error=selfRequest");
}else{
//CHECK THE CURRENT REQUESTS TO APPEND THE NEW REQUEST TO EXISTING ONES
$sql = "SELECT * FROM users WHERE `email` = ?;";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)){
header("location: index.php?login=quick&error=stmtfailed");
exit();
}
mysqli_stmt_bind_param($stmt, "s", $friendEmail);
mysqli_stmt_execute($stmt);
$resultData = mysqli_stmt_get_result($stmt);
if($row = mysqli_fetch_assoc($resultData)){
if($row["requests"] == "none"){
$toWrite = $user_id;
}else{
$allCurrRequests = explode(" ", $row["requests"]);
foreach($allCurrRequests as $element){
if($element == $user_id){
$inboxFull = true;
header("location: profile.php?error=inboxFull");
}
}
if($inboxFull == false){
$toWrite = $row["requests"] . " " . $user_id;
}
}
}else{
header("location: profile.php?error=noEmail");
exit();
}
mysqli_stmt_close($stmt);
//end sql
//APPEND THE NEW REQUEST
if($toWrite != 'nothing'){
$sql = "UPDATE `users` SET `requests`= ? WHERE `email`= ?";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)){
header("location: index.php?login=quick&error=stmtfailed");
exit();
}
mysqli_stmt_bind_param($stmt, "ss", $toWrite, $friendEmail);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
header("location: profile.php?response=sent");
mysqli_close($conn);
exit();
}
//ENDSQL//
}
}
?>