-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditBooks.php
More file actions
64 lines (57 loc) · 1.94 KB
/
editBooks.php
File metadata and controls
64 lines (57 loc) · 1.94 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
<?php
$conn = new mysqli("db", "root", "rootpassword", "library_db");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($_SERVER["REQUEST_METHOD"] === "GET" && isset($_GET["id"])) {
$id = (int) $_GET["id"];
$stmt = $conn->prepare("SELECT title, author, year FROM books WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$book = $stmt->get_result()->fetch_assoc();
if (!$book) {
echo "Book not found.";
exit;
}
?>
<form id="editForm">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<label>Title:
<input type="text" name="title" value="<?php echo htmlspecialchars($book['title']); ?>" required>
</label><br>
<label>Author:
<input type="text" name="author" value="<?php echo htmlspecialchars($book['author']); ?>" required>
</label><br>
<label>Year:
<input type="number" name="year" value="<?php echo htmlspecialchars($book['year']); ?>" required>
</label><br>
<button type="submit">Save Changes</button>
</form>
<?php
exit;
}
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["id"])) {
header("Content-Type: application/json"); // Set response type to JSON
$id = (int) $_POST["id"];
$title = trim($_POST["title"] ?? "");
$author = trim($_POST["author"] ?? "");
$year = (int) ($_POST["year"] ?? 0);
$stmt = $conn->prepare("UPDATE books SET title=?, author=?, year=? WHERE id=?");
$stmt->bind_param("ssii", $title, $author, $year, $id);
$ok = $stmt->execute();
if ($ok) {
echo json_encode([
"success" => true,
"id" => $id,
"title" => $title,
"author" => $author,
"year" => $year
]);
} else {
echo json_encode(["success" => false, "error" => $stmt->error]);
}
$stmt->close();
$conn->close();
exit;
}
?>