<?php
include('../includes/db.php');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Sanitize inputs
    $id = intval($_POST['id']);
    $status = $_POST['status'] ?? '';
    $code = $_POST['code'] ?? '';
    $name = $_POST['name'] ?? '';
    $modules = $_POST['modules'] ?? '';
    $duration = $_POST['duration'] ?? '';
    $description = $_POST['description'] ?? '';
    $course_description = $_POST['course_description'] ?? '';

    // Prepare SQL statement
    $sql = "UPDATE courses SET 
                status = ?, 
                code = ?, 
                course_name = ?, 
                modules = ?, 
                duration = ?, 
                description = ?, 
                course_description = ?
            WHERE id = ?";

    $stmt = $conn->prepare($sql);
    if (!$stmt) {
        echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
        exit;
    }

    // Corrected bind_param format: 7 strings + 1 integer = "sssssssi"
    $stmt->bind_param("sssssssi", 
        $status, 
        $code, 
        $name, 
        $modules, 
        $duration, 
        $description, 
        $course_description, 
        $id
    );

    if ($stmt->execute()) {
        echo "success";
    } else {
        echo "Database error: " . $stmt->error;
    }
} else {
    echo "Invalid request method.";
}
?>
