34352-vm/add_player.php
Flatlogic Bot 0f8fd03d51 1.3
2025-09-24 21:22:36 +00:00

46 lines
1.6 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$team_id = $_POST['team_id'];
$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']);
if (!empty($player_name) && !empty($player_email) && !empty($team_id)) {
try {
$pdo = db();
// Check if player already exists
$stmt = $pdo->prepare("SELECT id FROM players WHERE email = ?");
$stmt->execute([$player_email]);
$player = $stmt->fetch();
if ($player) {
$player_id = $player['id'];
} else {
// Insert new player
$stmt = $pdo->prepare("INSERT INTO players (name, email, high_school_year, season_year, team_id) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$player_name, $player_email, $high_school_year, $season_year, $team_id]);
$player_id = $pdo->lastInsertId();
}
// Update player's team_id if they already existed but weren't assigned to this team
$stmt = $pdo->prepare("UPDATE players SET team_id = ? WHERE id = ?");
$stmt->execute([$team_id, $player_id]);
$_SESSION['success_message'] = 'Player added successfully!';
} catch (PDOException $e) {
$_SESSION['error_message'] = 'Error adding player: ' . $e->getMessage();
}
}
header('Location: coach.php');
exit;
}
?>