-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_tournament.php
executable file
·95 lines (80 loc) · 2.62 KB
/
edit_tournament.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
<?php
include 'header.php';
require_once 'auth.php';
redirect_if_not_logged_in();
require_non_player();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Validate tournament ID
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
die("<p class='error'>Invalid tournament ID.</p>");
}
$tournament_id = intval($_GET['id']);
$user_id = $_SESSION['user_id'];
$is_admin = is_admin();
// Fetch tournament details
$query = $is_admin
? "SELECT * FROM tournaments WHERE id = ?"
: "SELECT * FROM tournaments WHERE id = ? AND created_by = ?";
$stmt = $conn->prepare($query);
if (!$stmt) {
die("<p class='error'>Database error: " . $conn->error . "</p>");
}
if ($is_admin) {
$stmt->bind_param("i", $tournament_id);
} else {
$stmt->bind_param("ii", $tournament_id, $user_id);
}
$stmt->execute();
$result = $stmt->get_result();
$tournament = $result->fetch_assoc();
$stmt->close();
if (!$tournament) {
die("<p class='error'>Tournament not found or access denied.</p>");
}
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'];
$year = intval($_POST['year']);
$query = "UPDATE tournaments SET name = ?, year = ? WHERE id = ?";
$stmt = $conn->prepare($query);
if (!$stmt) {
die("<p class='error'>Database error: " . $conn->error . "</p>");
}
$stmt->bind_param("sii", $name, $year, $tournament_id);
if ($stmt->execute()) {
echo "<p class='success'>Tournament updated successfully!</p>";
} else {
echo "<p class='error'>Error updating tournament: {$stmt->error}</p>";
}
$stmt->close();
header("Location: insert_tournament.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Tournament</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="top-bar">
<span>Welcome, <?= htmlspecialchars($_SESSION['username']) ?></span>
<a href="logout.php" class="logout-link">Logout</a>
</div>
<div class="container">
<h1>Edit Tournament</h1>
<form method="post">
<label for="name">Tournament Name:</label>
<input type="text" name="name" id="name" value="<?= htmlspecialchars($tournament['name']) ?>" required>
<label for="year">Year:</label>
<input type="number" name="year" id="year" value="<?= htmlspecialchars($tournament['year']) ?>" required>
<button type="submit">Save Changes</button>
</form>
</div>
</body>
</html>