95 lines
3.6 KiB
PHP
95 lines
3.6 KiB
PHP
<?php
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
session_start();
|
|
|
|
$player_id = $_GET['id'] ?? null;
|
|
|
|
if (!$player_id) {
|
|
header('Location: admin.php'); // Redirect to admin page if no player ID
|
|
exit;
|
|
}
|
|
|
|
$pdo = db();
|
|
|
|
// Fetch player details
|
|
$stmt = $pdo->prepare("SELECT * FROM players WHERE id = ?");
|
|
$stmt->execute([$player_id]);
|
|
$player = $stmt->fetch();
|
|
|
|
if (!$player) {
|
|
header('Location: admin.php'); // Redirect if player not found
|
|
exit;
|
|
}
|
|
|
|
// Fetch all teams
|
|
$teams_stmt = $pdo->query("SELECT id, team_name FROM teams ORDER BY team_name");
|
|
$teams = $teams_stmt->fetchAll();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$player_name = trim($_POST['player_name']);
|
|
$player_email = trim($_POST['player_email']);
|
|
$high_school_year = trim($_POST['high_school_year']);
|
|
$season_year = trim($_POST['season_year']);
|
|
$team_id = $_POST['team_id'] ?? null; // Get team_id from form
|
|
|
|
if (!empty($player_name) && !empty($player_email)) {
|
|
try {
|
|
// Update player details, including team_id
|
|
$stmt = $pdo->prepare("UPDATE players SET name = ?, email = ?, high_school_year = ?, season_year = ?, team_id = ? WHERE id = ?");
|
|
$stmt->execute([$player_name, $player_email, $high_school_year, $season_year, $team_id, $player_id]);
|
|
$_SESSION['success_message'] = 'Player updated successfully!';
|
|
} catch (PDOException $e) {
|
|
$_SESSION['error_message'] = 'Error updating player: ' . $e->getMessage();
|
|
}
|
|
}
|
|
|
|
header('Location: admin.php'); // Redirect back to admin page
|
|
exit;
|
|
}
|
|
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Edit Player</title>
|
|
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
|
|
</head>
|
|
<body>
|
|
<div class="container mt-5">
|
|
<h2>Edit Player</h2>
|
|
<form action="edit_player.php?id=<?php echo $player_id; ?>" method="post">
|
|
<div class="form-group">
|
|
<label for="player_name">Player Name</label>
|
|
<input type="text" name="player_name" id="player_name" class="form-control" value="<?php echo htmlspecialchars($player['name']); ?>" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="player_email">Player Email</label>
|
|
<input type="email" name="player_email" id="player_email" class="form-control" value="<?php echo htmlspecialchars($player['email']); ?>" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="high_school_year">High School Year</label>
|
|
<input type="text" name="high_school_year" id="high_school_year" class="form-control" value="<?php echo htmlspecialchars($player['high_school_year']); ?>">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="season_year">Season Year</label>
|
|
<input type="text" name="season_year" id="season_year" class="form-control" value="<?php echo htmlspecialchars($player['season_year']); ?>">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="team_id">Team</label>
|
|
<select name="team_id" id="team_id" class="form-control">
|
|
<option value="">Select a team</option>
|
|
<?php foreach ($teams as $team): ?>
|
|
<option value="<?php echo $team['id']; ?>" <?php echo ($player['team_id'] == $team['id']) ? 'selected' : ''; ?>>
|
|
<?php echo htmlspecialchars($team['team_name']); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Update Player</button>
|
|
</form>
|
|
</div>
|
|
</body>
|
|
</html>
|