Visa Källkod

The following files exist in this folder. Click to view.

NamnTypStorlek
account.phpPHP Fil11.6 KB
admin.phpPHP Fil23.6 KB
authors.phpPHP Fil4.1 KB
book.phpPHP Fil5.9 KB
db_cnnt.phpPHP Fil407 B
header.phpPHP Fil1.7 KB
home.phpPHP Fil8.4 KB
index.phpPHP Fil5.3 KB
loan_handler.phpPHP Fil1.9 KB
manage_authors.phpPHP Fil4.7 KB
my_loans.phpPHP Fil6.9 KB
S.sqlSQL Fil3.3 KB
search.phpPHP Fil11 KB

manage_authors.php

123 lines UTF-8 Unix (LF) - Type: PHP Fil
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php

include 'header.php';
require_once 
'db_cnnt.php';

// Kolla så det bara är admins som får vara här
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'Admin') {
    
header('Location: index.php?noaccess=1');
    exit;
}

global 
$pdo;
$message '';
$edit_author null;

// Om admin uppdaterar en författare (namn eller bio)
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_author'])) {
    
$author_id = (int)$_POST['author_id'];
    
$namn trim($_POST['namn'] ?? '');
    
$bio trim($_POST['bio'] ?? '');
    if (
$author_id && $namn) {
        
$pdo->prepare("UPDATE författare SET namn = ?, bio = ? WHERE författare_id = ?")->execute([$namn$bio$author_id]);
        
$message 'Författare uppdaterad!';
    }
}

// Om admin vill radera en författare
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_author'])) {
    
$author_id = (int)$_POST['author_id'];
    if (
$author_id) {
        
// kontrollera först om författaren har böcker (kan inte radera den då)
        
$statement $pdo->prepare("SELECT COUNT(*) as count FROM bok WHERE författare_id = ?");
        
$statement->execute([$author_id]);
        
$result $statement->fetch();
        if (
$result['count'] > 0) {
            
$message 'Kan inte radera författare som har böcker!';
        } else {
            
// går att radera,om inga böcker finns från denna författare
            
$pdo->prepare("DELETE FROM författare WHERE författare_id = ?")->execute([$author_id]);
            
$message 'Författare raderad!';
        }
    }
}

// Redigera en författare (från URL-parameter)
if (isset($_GET['edit'])) {
    
$edit_id = (int)$_GET['edit'];
    
$statement $pdo->prepare("SELECT * FROM författare WHERE författare_id = ?");
    
$statement->execute([$edit_id]);
    
$edit_author $statement->fetch();
}

// Hämta alla författare med hur många böcker de har
$statement $pdo->query("SELECT f.författare_id, f.namn, f.bio, COUNT(b.bok_id) as antal_böcker 
    FROM författare f 
    LEFT JOIN bok b ON f.författare_id = b.författare_id 
    GROUP BY f.författare_id 
    ORDER BY f.namn"
);
$authors $statement->fetchAll();
?>
<!DOCTYPE html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <title>Hantera författare</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
    <h1>Hantera författare</h1>
    <?php if ($message): ?>
        <div class="notice success"><?= htmlspecialchars($message?></div>
    <?php endif; ?>
    
    <?php if ($edit_author): ?>
        <div class="edit-section">
            <h2>Redigera: <?= htmlspecialchars($edit_author['namn']) ?></h2>
            <form method="post">
                <input type="hidden" name="author_id" value="<?= $edit_author['författare_id'?>">
                <input type="text" name="namn" value="<?= htmlspecialchars($edit_author['namn']) ?>" placeholder="Författarens namn" required>
                <textarea name="bio" rows="5" placeholder="Biografi..."><?= htmlspecialchars($edit_author['bio'] ?? ''?></textarea>
                <button type="submit" name="update_author">Spara ändringar</button>
                <a href="manage_authors.php" class="btn-cancel">Avbryt</a>
            </form>
        </div>
    <?php endif; ?>
    
    <h2>Alla författare</h2>
    <table>
        <thead>
            <tr>
                <th>Namn</th>
                <th>Antal böcker</th>
                <th>Biografi</th>
                <th>Åtgärder</th>
            </tr>
        </thead>
        <tbody>
        // tabell-rad för varje författare
            <?php foreach ($authors as $author): ?>
                <tr>
                    <td><?= htmlspecialchars($author['namn']) ?></td>
                    <td><?= $author['antal_böcker'?></td>
                    <td><?= !empty($author['bio']) ? substr(htmlspecialchars($author['bio']), 060) . '...' '<em>Ingen biografi</em>' ?></td>
                    <td>
                        <a href="manage_authors.php?edit=<?= $author['författare_id'?>" class="btn-small">Redigera</a>
                        <?php if ($author['antal_böcker'] == 0): ?>
                            <form method="post" style="display:inline;">
                                <input type="hidden" name="author_id" value="<?= $author['författare_id'?>">
                                <button type="submit" name="delete_author" class="btn-small btn-danger" onclick="return confirm('Radera denna författare?')">Radera</button>
                            </form>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    
    <a href="admin.php" class="btn-back">← Tillbaka till admin</a>
</div>
</body>
</html>