Blob: admin.php

Blob id: bf182b2ff1af9d8f50291c5090bd39ed60eedfbd

Size: 1.8 KB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php 
require_once "../src/database.php";

$admin = isset($_SESSION['user_id']);
$virhe = "";

// estä pääsy muilta paitsi opettajilta
if (!$admin) {
    echo "Sinulla ei ole riittäviä oikeuksia";
    die();
}

// lisää uusi aihe
if (isset($_POST['aihe']) && $_SERVER['REQUEST_METHOD'] === 'POST' && $admin) {
    $aihe = mysqli_real_escape_string($conn, trim($_POST['aihe'] ?? ''));

    // tarkastuksia
    if (strlen($aihe) < 2) {
        $virhe = "Aiheen pitää olla yli kaksi merkkiä pitkä";
    }

    if (strlen($aihe) > 255) {
        $virhe = "Aihe ei voi olla yli 255 merkkiä pitkä";
    }

    header("Location: " . $_SERVER['PHP_SELF']);
    exit();
}


// hae aiheet
$sql = "SELECT * FROM categories";
$result = $conn->query($sql);

$categories = [];
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $categories[] = $row;
    }
} else {
    echo "Aiheita ei löytynyt";
}

$conn->close();

?>

<!DOCTYPE html>
<html lang="fi">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Opettaja näkymä</title>
</head>
<body>
    <?php echo $_SESSION['user_name']; ?>
    <a href="../src/logout.php">
        Kirjaudu ulos
    </a>

    <table>
        <thead>
            <tr>
                <th>Aihe</th>
                <th>Opettaja</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($categories as $category): ?>
                <tr>
                    <td>
                        <?php echo $category["name"]; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <form action="" method="POST">
        <input type="hidden" name="aihe" value="i" />
        <button type="submit">
            Lisää aihe
        </button>
    </form>
</body>
</html>