-
Notifications
You must be signed in to change notification settings - Fork 0
/
logSearchTerm.php
63 lines (52 loc) · 1.75 KB
/
logSearchTerm.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
<?php
ini_set('display_errors', 0);
// Enable error logging
ini_set('log_errors', 1);
// Set the error reporting level
error_reporting(E_ALL);
$DBhostname = getenv("SQLHOSTNAME");
$usersDB = getenv("CSC130DBNAME");
$DBusername = getenv("CSC130DBUSER");
$DBpassword = getenv("CSC130DBPASS");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST") {
ob_start();
$searchTerm = trim($_POST['searchTerm']);
if(empty($searchTerm)){
http_response_code(200);
exit();
}
try{
$databaseConnection = mysqli_connect($DBhostname, $DBusername, $DBpassword, $usersDB);
if ($databaseConnection->connect_error) {
http_response_code(500);
ob_flush();
throw new Exception("Database Connection Error, Error No.: ".$databaseConnection->connect_errno." | ".$databaseConnection->connect_error);
}
$insertUserSearchQuery = mysqli_prepare($databaseConnection, "INSERT INTO `Search History` (`Search`) VALUES (?)");
mysqli_stmt_bind_param($insertUserSearchQuery, "s", $searchTerm);
if(mysqli_stmt_execute($insertUserSearchQuery) === TRUE){
mysqli_stmt_close($insertUserSearchQuery);
$databaseConnection->close();
http_response_code(200);
ob_flush();
exit();
} else{
http_response_code(500);
mysqli_stmt_close($insertUserSearchQuery);
$databaseConnection->close();
ob_flush();
exit();
}
}catch(Exception $e){
http_response_code(500);
echo "Internal Server Error: " . $e->getMessage();
ob_flush();
exit();
}
}else{
http_response_code(501);
ob_flush();
exit();
}
?>