-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_rate.php
159 lines (135 loc) · 5.51 KB
/
edit_rate.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
// Delete for Review implemented soxsun bu muellime gozune
include "dbconnection.php";
session_start();
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit;
}
// Get user data
$user_id = $_SESSION['user_id'];
if (!isset($_GET['game_id'])) {
die("Error: Game ID is not provided.");
}
$game_id = $_GET['game_id'];
// Ensure game_id is a valid number
if (!is_numeric($game_id)) {
die("Error: Invalid Game ID.");
}
// Fetch game details
$stmt = $conn->prepare("SELECT game_title, cover_image FROM Games WHERE game_id = ?");
$stmt->bind_param("i", $game_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$game = $result->fetch_assoc();
$game_title = htmlspecialchars($game['game_title']);
echo "<h3>Rate the game: $game_title</h3>";
} else {
echo "<p>No game found.</p>";
exit;
}
$stmt->close();
// Check if user has already reviewed this game
$stmt = $conn->prepare("
SELECT r.review_text, rat.rating
FROM Reviews r
LEFT JOIN Ratings rat ON r.game_id = rat.game_id AND r.user_id = rat.user_id
WHERE r.user_id = ? AND r.game_id = ?
");
$stmt->bind_param("ii", $user_id, $game_id);
$stmt->execute();
$result = $stmt->get_result();
$user_review = null;
$user_rating = null;
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$user_review = htmlspecialchars($row['review_text']);
$user_rating = (int)$row['rating'];
}
$stmt->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Review</title>
<style>
.submissionfield { width: 450px; height: 150px; border: 1px solid #999999; padding: 5px; }
.slider-label { font-weight: bold; }
</style>
</head>
<body>
<?php if ($user_review): ?>
<form action="" method="POST">
<label for="review">Edit your review:</label><br>
<textarea name="review" class="submissionfield" required><?= $user_review ?></textarea><br>
<label for="rating" class="slider-label">Update your rating (1 to 5):</label><br>
<input type="range" name="rating" min="1" max="5" step="1" value="<?= $user_rating ?>" required><br>
<input type="hidden" name="game_id" value="<?= $game_id ?>">
<input type="submit" name="submit" value="Update Review">
</form>
<form action="" method="POST">
<input type="hidden" name="game_id" value="<?= $game_id ?>">
<input type="submit" name="delete" value="Delete Review" style="color: red;">
</form>
<?php else: ?>
<p>You haven't submitted a review for this game yet.</p>
<?php endif; ?>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$game_id = filter_input(INPUT_POST, "game_id", FILTER_VALIDATE_INT);
if (isset($_POST['submit'])) {
$review = filter_input(INPUT_POST, "review", FILTER_SANITIZE_SPECIAL_CHARS);
$rating = filter_input(INPUT_POST, "rating", FILTER_VALIDATE_INT);
if (empty($review) || !$rating || !$game_id) {
echo "<p style='color: red;'>Invalid input. Please try again.</p>";
} else {
// Start a transaction
$conn->begin_transaction();
try {
$stmt = $conn->prepare("UPDATE Reviews SET review_text = ? WHERE user_id = ? AND game_id = ?");
$stmt->bind_param("sii", $review, $user_id, $game_id);
if (!$stmt->execute()) {
throw new Exception("Error updating review: " . $stmt->error);
}
$stmt->close();
$stmt = $conn->prepare("UPDATE Ratings SET rating = ? WHERE user_id = ? AND game_id = ?");
$stmt->bind_param("iii", $rating, $user_id, $game_id);
if (!$stmt->execute()) {
throw new Exception("Error updating rating: " . $stmt->error);
}
$conn->commit();
echo "<p style='color: green'>Review and rating updated successfully!</p>";
} catch (Exception $e) {
$conn->rollback();
echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
}
}
} elseif (isset($_POST['delete'])) {
try {
$conn->begin_transaction();
$stmt = $conn->prepare("
DELETE rv, rt
FROM Reviews rv
LEFT JOIN Ratings rt ON rv.game_id = rt.game_id AND rv.user_id = rt.user_id
WHERE rv.user_id = ? AND rv.game_id = ?
");
$stmt->bind_param("ii", $user_id, $game_id);
if (!$stmt->execute()) {
throw new Exception("Error deleting review: " . $stmt->error);
}
$conn->commit();
echo "<p style='color: green;'>Review and rating deleted successfully!</p>";
} catch (Exception $e) {
$conn->rollback();
echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
}
}
}
$conn->close();
?>