-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinsertViewed.php
73 lines (62 loc) · 2.35 KB
/
insertViewed.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
<?php
// insertViewed.php
// Shannen - this version is working with the changes that we made in the database
session_start();
include 'mylib.php'; // Include your database connection file
db_connect(); // Establish the database connection
global $db; // Use the global $db variable for the connection
// Check if the user is logged in
if (!isset($_SESSION['email'])) {
echo "User not logged in.";
exit();
}
// Get the logged-in user's email from the session
$userEmail = $_SESSION['email'];
// Fetch the UserID from the `users` table based on the email
$sql = "SELECT UserID FROM users WHERE email = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param("s", $userEmail);
$stmt->execute();
$stmt->bind_result($userID);
$stmt->fetch();
$stmt->close();
if (!$userID) {
echo "User not found.";
exit();
}
// Check if all the necessary fields are present in the POST request
if (isset($_POST['item_url']) && isset($_POST['item_title']) && isset($_POST['item_src'])) {
// Get the data from the POST request
$itemUrl = $_POST['item_url'];
$itemTitle = $_POST['item_title'];
$itemSrc = $_POST['item_src'];
// Check if the last viewed item for this user is the same as the new one
$sql = "SELECT itemUrl, itemTitle, itemSrc FROM viewed WHERE UserID = ? ORDER BY ViewedID DESC LIMIT 1";
$stmt = $db->prepare($sql);
$stmt->bind_param('i', $userID);
$stmt->execute();
$stmt->bind_result($lastItemUrl, $lastItemTitle, $lastItemSrc);
$stmt->fetch();
$stmt->close();
// If the last viewed item is the same as the current one, skip insertion
if ($lastItemUrl === $itemUrl && $lastItemTitle === $itemTitle && $lastItemSrc === $itemSrc) {
echo "This item has already been viewed recently.";
exit();
}
// Prepare the SQL statement to insert the viewed item into the database
$sql = "INSERT INTO viewed (UserID, itemUrl, itemTitle, itemSrc) VALUES (?, ?, ?, ?)";
$stmt = $db->prepare($sql);
$stmt->bind_param('isss', $userID, $itemUrl, $itemTitle, $itemSrc);
// Execute the query and check if it was successful
if ($stmt->execute()) {
echo "Item successfully added to viewed history.";
} else {
echo "Error adding item: " . $db->error;
}
// Close the statement and the connection
$stmt->close();
$db->close();
} else {
echo "Missing required parameters.";
}
?>