<?php
require_once "includes/auth.php";
require_once "includes/db.php";

$userId = $_SESSION["user_id"];

if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["course_id"])) {
    $courseId = (int) $_POST["course_id"];

    // Daha önce kaydolmuş mu?
    $stmt = $conn->prepare("SELECT * FROM enrollments WHERE user_id = ? AND course_id = ?");
    $stmt->execute([$userId, $courseId]);

    if ($stmt->rowCount() === 0) {
        // Kayıt işlemi
        $stmt = $conn->prepare("INSERT INTO enrollments (user_id, course_id) VALUES (?, ?)");
        $stmt->execute([$userId, $courseId]);
    }
}

// Yönlendir
header("Location: dashboard.php");
exit;
