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; } ?>