-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_book.php
More file actions
executable file
·90 lines (74 loc) · 2.29 KB
/
edit_book.php
File metadata and controls
executable file
·90 lines (74 loc) · 2.29 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
87
88
89
90
<?php
session_start();
if (isset($_COOKIE['admin'])) {
$_SESSION['admin'] = $_COOKIE['admin'];
}
if (!isset($_SESSION['admin'])) {
header("Location: login.php");
exit();
}
?>
<?php
/*
// Include database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "shehda_library_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
*/
// include "db_connect.php";
require_once "db_connect.php"; // require once is better than include, because the database connection is important.
// Check if book ID is provided
if (!isset($_GET['id']) || empty($_GET['id'])) {
die("Invalid book ID.");
}
$book_id = $_GET['id'];
// Fetch book details
$sql = "SELECT * FROM books WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $book_id);
$stmt->execute();
$result = $stmt->get_result();
$book = $result->fetch_assoc();
if (!$book) {
die("Book not found.");
}
// Update book details (Only Title & Author)
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$title = $_POST['title'];
$author = $_POST['author'];
$update_sql = "UPDATE books SET title=?, author=? WHERE id=?";
$stmt = $conn->prepare($update_sql);
$stmt->bind_param("ssi", $title, $author, $book_id);
if ($stmt->execute()) {
echo "<script>alert('Book updated successfully!'); window.location='index.php';</script>";
} else {
echo "Error updating book: " . $conn->error;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Book</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h2>Edit Book</h2>
<form method="post">
<label>Title:</label>
<input type="text" name="title" value="<?php echo htmlspecialchars($book['title']); ?>" required>
<label>Author:</label>
<input type="text" name="author" value="<?php echo htmlspecialchars($book['author']); ?>" required>
<button type="submit">Update Book</button>
</form>
<a class="a1" href="index.php">Back to Library</a>
</body>
</html>
<?php $conn->close(); ?>