diff --git a/add_player.php b/add_player.php new file mode 100644 index 0000000..f56cf27 --- /dev/null +++ b/add_player.php @@ -0,0 +1,44 @@ +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) VALUES (?, ?, ?, ?)"); + $stmt->execute([$player_name, $player_email, $high_school_year, $season_year]); + $player_id = $pdo->lastInsertId(); + } + + // Add player to team + $stmt = $pdo->prepare("INSERT INTO team_members (team_id, player_id) VALUES (?, ?)"); + $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; +} +?> \ No newline at end of file diff --git a/add_score.php b/add_score.php index 89df036..778d2a9 100644 --- a/add_score.php +++ b/add_score.php @@ -1,14 +1,19 @@ query("SELECT id, name FROM courses ORDER BY name"); $courses = $stmt->fetchAll(); + + $stmt = $pdo->query("SELECT p.id, p.player_name, t.team_name FROM players p JOIN teams t ON p.team_id = t.id ORDER BY t.team_name, p.player_name"); + $players = $stmt->fetchAll(); } catch (PDOException $e) { - $courses = []; - // Silently fail, the JS will handle the empty state + // If something goes wrong, we'll have empty arrays. + // For debugging, you might want to log the error: + // error_log($e->getMessage()); } ?> @@ -50,12 +55,27 @@ try {
- - -
-
- - + +
diff --git a/assets/js/main.js b/assets/js/main.js index 1506c7a..ffb45e2 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -169,9 +169,10 @@ document.addEventListener('DOMContentLoaded', function () { form.addEventListener('submit', async function(e) { e.preventDefault(); - const playerName = document.getElementById('playerName').value; - if (!playerName) { - alert('Player name is required.'); + const playerSelect = document.getElementById('playerSelect'); + const playerId = playerSelect.value; + if (!playerId) { + alert('Please select a player.'); return; } @@ -182,8 +183,7 @@ document.addEventListener('DOMContentLoaded', function () { } const data = { - playerName: playerName, - teamName: document.getElementById('teamName').value, + playerId: playerId, courseId: courseSelect.value, holes: holes, scores: scores, @@ -210,6 +210,7 @@ document.addEventListener('DOMContentLoaded', function () { } form.reset(); courseSelect.value = ''; + playerSelect.value = ''; coursePars = null; renderTable(); toggleInputs(true); diff --git a/coach.php b/coach.php index eebaab6..3cb7d37 100644 --- a/coach.php +++ b/coach.php @@ -1,6 +1,8 @@ prepare('SELECT * FROM teams WHERE coach_id = ?'); $stmt->execute([$coach_id]); $teams = $stmt->fetchAll(); +$success_message = $_SESSION['success_message'] ?? null; +$error_message = $_SESSION['error_message'] ?? null; + +unset($_SESSION['success_message']); +unset($_SESSION['error_message']); + ?> @@ -41,48 +49,107 @@ $teams = $stmt->fetchAll();

Coach's Dashboard

+ +
+ +
+ + + +
+ +
+ + +
+
+

Create New Team

+
+
+ + +
+
+
+
+

Team:

+ +

Players

prepare(' - SELECT p.name as player_name, s.id as score_id, s.total_score, c.name as course_name, s.played_at - FROM scores s - JOIN players p ON s.player_id = p.id - JOIN courses c ON s.course_id = c.id - WHERE s.team_id = ? - ORDER BY s.played_at DESC - '); + $stmt = $pdo->prepare('SELECT p.* FROM players p JOIN team_members tm ON p.id = tm.player_id WHERE tm.team_id = ?'); $stmt->execute([$team['id']]); - $scores = $stmt->fetchAll(); + $players = $stmt->fetchAll(); ?> - + - - - - - + + + + + - + - - - - - + + + + +
PlayerCourseScoreDateActionNameEmailHigh School YearSeason YearActions
Edit + Edit + Delete +
-

No scores recorded for this team yet.

+

No players in this team yet.

+ +
+ +

Add Player to Team

+
+ +
+
+
+ + +
+
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+ + +
+
+
+ +
+
@@ -97,4 +164,4 @@ $teams = $stmt->fetchAll(); - \ No newline at end of file + diff --git a/create_team.php b/create_team.php new file mode 100644 index 0000000..51b9f0e --- /dev/null +++ b/create_team.php @@ -0,0 +1,26 @@ +prepare("INSERT INTO teams (name, coach_id) VALUES (?, ?)"); + $stmt->execute([$team_name, $coach_id]); + $_SESSION['success_message'] = 'Team created successfully!'; + } catch (PDOException $e) { + $_SESSION['error_message'] = 'Error creating team: ' . $e->getMessage(); + } + } + + header('Location: coach.php'); + exit; +} +?> \ No newline at end of file diff --git a/db/migrations/20250924_add_player_details.php b/db/migrations/20250924_add_player_details.php new file mode 100644 index 0000000..e2c018d --- /dev/null +++ b/db/migrations/20250924_add_player_details.php @@ -0,0 +1,15 @@ +exec(" + ALTER TABLE players + ADD COLUMN high_school_year VARCHAR(255), + ADD COLUMN season_year VARCHAR(255); + "); + echo "Table 'players' updated successfully with 'high_school_year' and 'season_year' columns.\n"; +} catch (PDOException $e) { + die("DB ERROR: " . $e->getMessage()); +} +?> diff --git a/delete_player.php b/delete_player.php new file mode 100644 index 0000000..4c748e1 --- /dev/null +++ b/delete_player.php @@ -0,0 +1,22 @@ +prepare("DELETE FROM team_members WHERE player_id = ? AND team_id = ?"); + $stmt->execute([$player_id, $team_id]); + $_SESSION['success_message'] = 'Player removed from team successfully!'; + } catch (PDOException $e) { + $_SESSION['error_message'] = 'Error removing player: ' . $e->getMessage(); + } +} + +header('Location: coach.php'); +exit; +?> \ No newline at end of file diff --git a/edit_player.php b/edit_player.php new file mode 100644 index 0000000..a54351f --- /dev/null +++ b/edit_player.php @@ -0,0 +1,76 @@ +prepare("SELECT * FROM players WHERE id = ?"); +$stmt->execute([$player_id]); +$player = $stmt->fetch(); + +if (!$player) { + header('Location: coach.php'); + exit; +} + +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']); + + if (!empty($player_name) && !empty($player_email)) { + try { + $stmt = $pdo->prepare("UPDATE players SET name = ?, email = ?, high_school_year = ?, season_year = ? WHERE id = ?"); + $stmt->execute([$player_name, $player_email, $high_school_year, $season_year, $player_id]); + $_SESSION['success_message'] = 'Player updated successfully!'; + } catch (PDOException $e) { + $_SESSION['error_message'] = 'Error updating player: ' . $e->getMessage(); + } + } + + header('Location: coach.php'); + exit; +} + +?> + + + + + + Edit Player + + + +
+

Edit Player

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+
+ + \ No newline at end of file diff --git a/submit_score.php b/submit_score.php index 06ea67d..ebfd4ad 100644 --- a/submit_score.php +++ b/submit_score.php @@ -71,7 +71,7 @@ if (!$data) { exit; } -$required_fields = ['playerName', 'courseId', 'holes', 'scores']; +$required_fields = ['playerId', 'courseId', 'holes', 'scores']; foreach ($required_fields as $field) { if (empty($data[$field])) { http_response_code(400); @@ -83,14 +83,23 @@ foreach ($required_fields as $field) { try { $pdo = db(); - // This part needs to be updated to use player_id and team_id - // For now, it will fail if player_name column is removed as per migration. - // This should be addressed in a future step. + // Get team_id from player_id + $stmt = $pdo->prepare("SELECT team_id FROM players WHERE id = ?"); + $stmt->execute([$data['playerId']]); + $player = $stmt->fetch(); - $sql = "INSERT INTO scores (player_name, team_name, course_id, holes_played, total_score, total_to_par"; + if (!$player) { + http_response_code(404); + echo json_encode(['error' => 'Player not found']); + exit; + } + + $team_id = $player['team_id']; + + $sql = "INSERT INTO scores (player_id, team_id, course_id, holes_played, total_score, total_to_par"; $params = [ - ':player_name' => $data['playerName'], - ':team_name' => $data['teamName'] ?? null, + ':player_id' => $data['playerId'], + ':team_id' => $team_id, ':course_id' => $data['courseId'], ':holes_played' => $data['holes'], ':total_score' => $data['totalScore'],