-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprofilebackendaddPref.php
124 lines (91 loc) · 3.75 KB
/
profilebackendaddPref.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
require_once "connect.php";
session_start();
//This function checks if any of the fields are empty and returns false if they are
function missingFields($username){
if (strlen($username) == 0){
$message = "Please fill out all fields";
popUp($message);
return false;
}
return true;
}
//This function gets the account_id of the user from the database
function accountIDlookup($username){
global $conn;
$sql = "SELECT account_id FROM logins WHERE username = '$username'";
$result = $conn->query($sql);
if ($result->num_rows > 0){
$row = $result->fetch_assoc();
$account_id = $row["account_id"];
return $account_id;
}
}
//This function updates the preferences in the database for the user when UPDATES is specified or
//adds the account id of the user and their preferences to the database when ADD is specified
function updatePreferences($rock,$pop,$country,$jazz,$classical,$folk,$indie,$alt,$metal,$account_id,$type){
global $conn;
if ($type == "ADD"){
$sql = "INSERT INTO preferences (account_id, rock, pop, country, jazz, classical, folk, indie, alt, metal)
VALUES ('$account_id', '$rock', '$pop', '$country', '$jazz', '$classical', '$folk', '$indie', '$alt', '$metal')";
$conn->query($sql);
}
if ($type == "UPDATES"){
$sql = "UPDATE preferences SET rock='$rock' WHERE account_id='$account_id'";
$conn->query($sql);
$sql = "UPDATE preferences SET pop='$pop' WHERE account_id='$account_id'";
$conn->query($sql);
$sql = "UPDATE preferences SET country='$country' WHERE account_id='$account_id'";
$conn->query($sql);
$sql = "UPDATE preferences SET jazz='$jazz' WHERE account_id='$account_id'";
$conn->query($sql);
$sql = "UPDATE preferences SET classical='$classical' WHERE account_id='$account_id'";
$conn->query($sql);
$sql = "UPDATE preferences SET folk='$folk' WHERE account_id='$account_id'";
$conn->query($sql);
$sql = "UPDATE preferences SET indie='$indie' WHERE account_id='$account_id'";
$conn->query($sql);
$sql = "UPDATE preferences SET alt='$alt' WHERE account_id='$account_id'";
$conn->query($sql);
$sql = "UPDATE preferences SET metal='$metal' WHERE account_id='$account_id'";
$conn->query($sql);
}
}
//This function will take in a username, password, email, and alt_email and create a new user
//in the database. If the user already exists, it will return false, it will not create a new user.
function profileAddPrefPSQL(){
global $profilePath;
global $conn;
if (isset($_SESSION["logged_in"]) == false){
$message = "Please login to access this page";
popUp($message);
redirectPage($profilePath);
//exit();
}
$username = $_SESSION["username"];
if (missingFields($username) == false) {
redirectPage($profilePath);
}
$rock = getInfo("Rock");
$pop = getInfo("Pop");
$country = getInfo("Country");
$jazz = getInfo("Jazz");
$classical = getInfo("Classical");
$folk = getInfo("Folk");
$indie = getInfo("Indie");
$alt = getInfo("Alt");
$metal = getInfo("Metal");
$account_id = accountIDlookup($username);
$sql = "SELECT account_id FROM preferences WHERE account_id = '$account_id'";
$result = $conn->query($sql);
if ($result->num_rows > 0){
updatePreferences($rock,$pop,$country,$jazz,$classical, $folk,$indie,$alt,$metal,$account_id,"UPDATES");
}else{
updatePreferences($rock,$pop,$country,$jazz,$classical,$folk,$indie,$alt,$metal,$account_id,"ADD");
}
$message = "Preferences Updated";
popUp($message);
redirectPage($profilePath);
}
profileAddPrefPSQL();
?>