-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_book_details.php
More file actions
40 lines (33 loc) · 1.2 KB
/
get_book_details.php
File metadata and controls
40 lines (33 loc) · 1.2 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
<?php
// Verbindung zur Datenbank herstellen
include('db-files/db-conect.php');
// Überprüfen, ob eine ID übergeben wurde
if (isset($_GET['id'])) {
$book_id = intval($_GET['id']);
// SQL-Abfrage, um das Buch basierend auf der ID abzurufen
$query = $pdo->prepare("SELECT * FROM books WHERE id = :id");
$query->bindParam(':id', $book_id, PDO::PARAM_INT);
$query->execute();
$book = $query->fetch(PDO::FETCH_ASSOC);
if ($book) {
// SQL-Abfrage, um die Genres basierend auf der Buch-ID abzurufen
$genre_query = $pdo->prepare("
SELECT g.name
FROM genres g
INNER JOIN book_genres bg ON g.id = bg.genre_id
WHERE bg.book_id = :book_id
");
$genre_query->bindParam(':book_id', $book_id, PDO::PARAM_INT);
$genre_query->execute();
$genres = $genre_query->fetchAll(PDO::FETCH_COLUMN);
// Füge die Genres zum Buch-Array hinzu
$book['genres'] = $genres;
// Buchdetails als JSON zurückgeben
echo json_encode($book);
} else {
echo json_encode(['error' => 'Buch nicht gefunden.']);
}
} else {
echo json_encode(['error' => 'Keine Buch-ID übergeben.']);
}
?>