-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_results_gd.php
executable file
·252 lines (233 loc) · 9.45 KB
/
edit_results_gd.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
// Start output buffering
ob_start();
include 'header.php';
require_once 'conn.php';
require_non_player();
// Ensure the user is logged in
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
if (!isset($_SESSION['user_id'])) {
die("Error: User not logged in.");
}
$user_id = $_SESSION['user_id'];
// Database connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Database connection failed: " . $conn->connect_error);
}
// Define stages
$stages = ['Pre Quarter Finals','Quarter Finals','Semifinals','Finals','Preliminary'];
// Redirect helper function
function redirect_with_message($location, $message)
{
header("Location: $location?$message");
exit;
}
// Handle POST requests
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['edit_match'])) {
$match_id = $_POST['match_id'] ?? null;
$stage = $_POST['stage'] ?? null;
$match_date = $_POST['match_date'] ?? null;
$match_time = $_POST['match_time'] ?? null;
$set1_team1_points = $_POST['set1_team1_points'] ?? 0;
$set1_team2_points = $_POST['set1_team2_points'] ?? 0;
$set2_team1_points = $_POST['set2_team1_points'] ?? 0;
$set2_team2_points = $_POST['set2_team2_points'] ?? 0;
$set3_team1_points = $_POST['set3_team1_points'] ?? 0;
$set3_team2_points = $_POST['set3_team2_points'] ?? 0;
if (!$match_id || !$stage || !$match_date || !$match_time) {
redirect_with_message('edit_results_gd.php', 'error=missing_fields');
}
if (!in_array($stage, $stages)) {
redirect_with_message('edit_results_gd.php', 'error=invalid_stage');
}
// Update query with access control
$update_query = "
UPDATE matches SET
stage = ?,
match_date = ?,
match_time = ?,
set1_team1_points = ?,
set1_team2_points = ?,
set2_team1_points = ?,
set2_team2_points = ?,
set3_team1_points = ?,
set3_team2_points = ?
WHERE id = ? AND (created_by = ? OR EXISTS (
SELECT 1 FROM tournaments t
INNER JOIN tournament_moderators tm ON t.id = tm.tournament_id
WHERE t.id = matches.tournament_id AND tm.user_id = ?
))
";
$stmt = $conn->prepare($update_query);
if ($stmt) {
$stmt->bind_param(
"sssiiiiiiiii",
$stage,
$match_date,
$match_time,
$set1_team1_points,
$set1_team2_points,
$set2_team1_points,
$set2_team2_points,
$set3_team1_points,
$set3_team2_points,
$match_id,
$user_id,
$user_id
);
$stmt->execute();
if ($stmt->affected_rows > 0) {
redirect_with_message('results_gd.php', 'success=match_updated');
} else {
redirect_with_message('results_gd.php', 'error=no_changes');
}
} else {
redirect_with_message('edit_results_gd.php', 'error=query_failed');
}
}
if (isset($_POST['delete_match'])) {
$match_id = $_POST['match_id'] ?? null;
if ($match_id) {
// Delete query with access control
$delete_query = "
DELETE FROM matches
WHERE id = ? AND (created_by = ? OR EXISTS (
SELECT 1 FROM tournaments t
INNER JOIN tournament_moderators tm ON t.id = tm.tournament_id
WHERE t.id = matches.tournament_id AND tm.user_id = ?
))
";
$stmt = $conn->prepare($delete_query);
if ($stmt) {
$stmt->bind_param("iii", $match_id, $user_id, $user_id);
$stmt->execute();
if ($stmt->affected_rows > 0) {
redirect_with_message('results_gd.php', 'success=match_deleted');
} else {
redirect_with_message('results_gd.php', 'error=delete_failed');
}
} else {
redirect_with_message('edit_results_gd.php', 'error=query_failed');
}
}
}
}
// Fetch matches for Girls Doubles with access control
$query = "
SELECT
m.id AS match_id,
t.name AS tournament_name,
c.name AS category_name,
p1.name AS team1_player1_name,
p2.name AS team1_player2_name,
p3.name AS team2_player1_name,
p4.name AS team2_player2_name,
m.stage,
m.match_date,
m.match_time,
m.set1_team1_points,
m.set1_team2_points,
m.set2_team1_points,
m.set2_team2_points,
m.set3_team1_points,
m.set3_team2_points,
m.created_by
FROM matches m
INNER JOIN tournaments t ON m.tournament_id = t.id
INNER JOIN categories c ON m.category_id = c.id
LEFT JOIN players p1 ON m.team1_player1_id = p1.id
LEFT JOIN players p2 ON m.team1_player2_id = p2.id
LEFT JOIN players p3 ON m.team2_player1_id = p3.id
LEFT JOIN players p4 ON m.team2_player2_id = p4.id
WHERE c.type = 'doubles' AND c.sex = 'F'
AND (m.created_by = ? OR EXISTS (
SELECT 1 FROM tournaments t
INNER JOIN tournament_moderators tm ON t.id = tm.tournament_id
WHERE t.id = m.tournament_id AND tm.user_id = ?
))
ORDER BY m.id
";
$stmt = $conn->prepare($query);
$stmt->bind_param("ii", $user_id, $user_id);
$stmt->execute();
$result = $stmt->get_result();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Girls Doubles Match Results</title>
</head>
<body>
<h1>Girls Doubles Match Results</h1>
<?php if ($result->num_rows > 0): ?>
<table>
<tr>
<th>Match ID</th>
<th>Tournament</th>
<th>Category</th>
<th>Team 1</th>
<th>Team 2</th>
<th>Stage</th>
<th>Match Date</th>
<th>Match Time</th>
<th>Set 1 (Team 1 - Team 2)</th>
<th>Set 2 (Team 1 - Team 2)</th>
<th>Set 3 (Team 1 - Team 2)</th>
<th>Winner</th>
<th>Actions</th>
</tr>
<?php while ($row = $result->fetch_assoc()): ?>
<form method="post">
<tr>
<td><?= $row['match_id'] ?><input type="hidden" name="match_id" value="<?= $row['match_id'] ?>"></td>
<td><?= $row['tournament_name'] ?></td>
<td><?= $row['category_name'] ?></td>
<td><?= $row['team1_player1_name'] . " & " . $row['team1_player2_name'] ?></td>
<td><?= $row['team2_player1_name'] . " & " . $row['team2_player2_name'] ?></td>
<td>
<select name="stage">
<?php foreach ($stages as $stage): ?>
<option value="<?= $stage ?>" <?= $row['stage'] === $stage ? 'selected' : '' ?>><?= $stage ?></option>
<?php endforeach; ?>
</select>
</td>
<td><input type="date" name="match_date" value="<?= $row['match_date'] ?>"></td>
<td><input type="time" name="match_time" value="<?= $row['match_time'] ?>"></td>
<td>
<input type="number" name="set1_team1_points" value="<?= $row['set1_team1_points'] ?>" style="width: 50px;"> -
<input type="number" name="set1_team2_points" value="<?= $row['set1_team2_points'] ?>" style="width: 50px;">
</td>
<td>
<input type="number" name="set2_team1_points" value="<?= $row['set2_team1_points'] ?>" style="width: 50px;"> -
<input type="number" name="set2_team2_points" value="<?= $row['set2_team2_points'] ?>" style="width: 50px;">
</td>
<td>
<input type="number" name="set3_team1_points" value="<?= $row['set3_team1_points'] ?>" style="width: 50px;"> -
<input type="number" name="set3_team2_points" value="<?= $row['set3_team2_points'] ?>" style="width: 50px;">
</td>
<td>
<?= ($row['set1_team1_points'] + $row['set2_team1_points'] + $row['set3_team1_points']) >
($row['set1_team2_points'] + $row['set2_team2_points'] + $row['set3_team2_points'])
? 'Team 1' : 'Team 2' ?>
</td>
<td>
<button type="submit" name="edit_match">Edit</button>
<button type="submit" name="delete_match" onclick="return confirm('Are you sure?')">Delete</button>
</td>
</tr>
</form>
<?php endwhile; ?>
</table>
<?php else: ?>
<p>No matches found.</p>
<?php endif; ?>
<?php $conn->close(); ?>
</body>
</html>
<?php $conn->close(); ?>