<?php
require_once 'config.php';

// Insert mark
if (isset($_POST['insert_mark'])) {
    $student_id = $_POST['student_id'];
    $subject_id = $_POST['subject_id'];
    $mark = $_POST['mark'];

    $query = "INSERT INTO results (student_id, subject_id, mark) VALUES ('$student_id', '$subject_id', '$mark')";
    $conn->query($query);

    header('Location: admin_panel.php');
    exit;
}

// Add subject
if (isset($_POST['add_subject'])) {
    $name = $_POST['name'];

    $query = "INSERT INTO subjects (name) VALUES ('$name')";
    $conn->query($query);

    header('Location: admin_panel.php');
    exit;
}

// Show all student results
if (isset($_POST['show_results'])) {
    $query = "SELECT * FROM results";
    $result = $conn->query($query);

    if ($result->num_rows > 0) {
        echo "<h2>All Student Results:</h2>";
        echo "<table border='1'>";
        echo "<tr><th>Student ID</th><th>Subject ID</th><th>Mark</th></tr>";
        while ($row = $result->fetch_assoc()) {
            echo "<tr><td>". $row['student_id']. "</td><td>". $row['subject_id']. "</td><td>". $row['mark']. "</td></tr>";
        }
        echo "</table>";
    } else {
        echo "No results found";
    }
}

// Filter student results
if (isset($_POST['filter_results'])) {
    $student_id = $_POST['student_id'];

    $query = "SELECT * FROM results WHERE student_id = '$student_id'";
    $result = $conn->query($query);

    if ($result->num_rows > 0) {
        echo "<h2>Filtered Student Results:</h2>";
        echo "<table border='1'>";
        echo "<tr><th>Student ID</th><th>Subject ID</th><th>Mark</th></tr>";
        while ($row = $result->fetch_assoc()) {
            echo "<tr><td>". $row['student_id']. "</td><td>". $row['subject_id']. "</td><td>". $row['mark']. "</td></tr>";
        }
        echo "</table>";
    } else {
        echo "No results found";
    }
}
?>

<!-- HTML code for admin panel -->
<html>
<head>
    <title>Admin Panel</title>
</head>
<body>
    <h1>Admin Panel</h1>
    <h2>Insert Mark:</h2>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
        <label>Student ID:</label>
        <input type="number" name="student_id" required><br><br>
        <label>Subject ID:</label>
        <input type="number" name="subject_id" required><br><br>
        <label>Mark:</label>
        <input type="number" name="mark" required><br><br>
        <input type="submit" name="insert_mark" value="Insert">
    </form>

    <h2>Add Subject:</h2>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
        <label>Subject Name:</label>
        <input type="text" name="name" required><br><br>
        <input type="submit" name="add_subject" value="Add">
    </form>

    <h2>Show All Student Results:</h2>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
        <input type="submit" name="show_results" value="Show">
    </form>

    <h2>Filter Student Results:</h2>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
        <label>Student ID:</label>
        <input type="number" name="student_id" required><br><br>
        <input type="submit" name="filter_results" value="Filter">
    </form>
</body>
</html>