diff --git a/db/migrations/002_create_competition_participants.sql b/db/migrations/002_create_competition_participants.sql new file mode 100644 index 0000000..d9b5dca --- /dev/null +++ b/db/migrations/002_create_competition_participants.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS `competition_participants` ( + `id` INT AUTO_INCREMENT PRIMARY KEY, + `user_id` INT NOT NULL, + `competition_id` INT NOT NULL, + `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE, + FOREIGN KEY (`competition_id`) REFERENCES `competitions`(`id`) ON DELETE CASCADE, + UNIQUE KEY `user_competition` (`user_id`, `competition_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; diff --git a/db/migrations/003_create_competition_submissions.sql b/db/migrations/003_create_competition_submissions.sql new file mode 100644 index 0000000..888f991 --- /dev/null +++ b/db/migrations/003_create_competition_submissions.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS `competition_submissions` ( + `id` INT AUTO_INCREMENT PRIMARY KEY, + `user_id` INT NOT NULL, + `competition_id` INT NOT NULL, + `file_path` VARCHAR(255) NOT NULL, + `uploaded_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE, + FOREIGN KEY (`competition_id`) REFERENCES `competitions`(`id`) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; diff --git a/join_competition.php b/join_competition.php new file mode 100644 index 0000000..094468a --- /dev/null +++ b/join_competition.php @@ -0,0 +1,25 @@ +prepare("INSERT INTO competition_participants (user_id, competition_id) VALUES (?, ?)"); + $stmt->execute([$user_id, $competition_id]); + } catch (PDOException $e) { + // Handle potential errors, like trying to join the same competition twice + // You might want to log this error or show a message + } +} + +header("location: user_dashboard.php"); +exit; diff --git a/login.php b/login.php index 8af845c..121b1b8 100644 --- a/login.php +++ b/login.php @@ -23,6 +23,8 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { $stmt->execute([$email]); $user = $stmt->fetch(PDO::FETCH_ASSOC); + + if ($user && password_verify($password, $user['password'])) { $_SESSION['user_id'] = $user['id']; $_SESSION['user_name'] = $user['name']; @@ -125,7 +127,7 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
-
+
@@ -140,4 +142,4 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
- + \ No newline at end of file diff --git a/register.php b/register.php index 7022107..d34ff04 100644 --- a/register.php +++ b/register.php @@ -137,7 +137,7 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
- +
diff --git a/upload_submission.php b/upload_submission.php new file mode 100644 index 0000000..889dcb9 --- /dev/null +++ b/upload_submission.php @@ -0,0 +1,54 @@ +prepare("INSERT INTO competition_submissions (user_id, competition_id, file_path) VALUES (?, ?, ?)"); + $stmt->execute([$user_id, $competition_id, $file_path]); + } catch (PDOException $e) { + // Handle DB error + // You might want to log this error + // If DB insert fails, you might want to delete the uploaded file + unlink($file_path); + header("location: user_dashboard.php?upload_error=2"); + exit; + } + } + + header("location: user_dashboard.php"); + exit; +} + +header("location: user_dashboard.php"); +exit; diff --git a/user_dashboard.php b/user_dashboard.php index fd8491f..1cc3a0f 100644 --- a/user_dashboard.php +++ b/user_dashboard.php @@ -11,13 +11,27 @@ if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'user') { exit; } -// Fetch competitions from the database +$user_id = $_SESSION['user_id']; + +// Fetch competitions, participations, and submissions try { $pdo = db(); - $stmt = $pdo->query('SELECT title, description, start_date FROM competitions ORDER BY start_date ASC'); + + // Fetch all competitions + $stmt = $pdo->query('SELECT id, title, description, start_date FROM competitions ORDER BY start_date ASC'); $competitions = $stmt->fetchAll(PDO::FETCH_ASSOC); + + // Fetch competitions the user has joined + $stmt = $pdo->prepare('SELECT competition_id FROM competition_participants WHERE user_id = ?'); + $stmt->execute([$user_id]); + $joined_competitions = $stmt->fetchAll(PDO::FETCH_COLUMN, 0); + + // Fetch user's submissions + $stmt = $pdo->prepare('SELECT competition_id, file_path, uploaded_at FROM competition_submissions WHERE user_id = ?'); + $stmt->execute([$user_id]); + $submissions = $stmt->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_ASSOC); + } catch (PDOException $e) { - // Handle DB error $competitions = []; $db_error = "Failed to load competitions."; } @@ -65,6 +79,35 @@ try {

Starts on:

+ + +
+
Your Submissions:
+ +
    + +
  • ()
  • + +
+ +

No submissions yet.

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