-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateBook.php
More file actions
94 lines (78 loc) · 3.09 KB
/
updateBook.php
File metadata and controls
94 lines (78 loc) · 3.09 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
91
92
93
94
<?php
// Connect to the database
$servername = "localhost";
$username = "root"; // Update with your database username
$password = ""; // Update with your database password
$dbname = "library_db"; // Update with your database name
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Check if the form is submitted to fetch book details
if (isset($_POST['fetch_book'])) {
$bookId = $_POST['book_id'];
// Prepare SQL query to fetch the book by ID
$stmt = $conn->prepare("SELECT * FROM books WHERE id = ?");
$stmt->bind_param("i", $bookId);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
// Fetch the book details
$book = $result->fetch_assoc();
// Show the form with the book details for editing
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Update Book</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="form-container">
<h1>Update Book</h1>
<form method="POST" action="updateBook.php">
<input type="hidden" name="id" value="<?php echo $book['id']; ?>" />
<label for="title">Book Title:</label>
<input type="text" id="title" name="title" value="<?php echo $book['title']; ?>" required><br><br>
<label for="author">Author:</label>
<input type="text" id="author" name="author" value="<?php echo $book['author']; ?>" required><br><br>
<label for="genre">Genre:</label>
<input type="text" id="genre" name="genre" value="<?php echo $book['genre']; ?>" required><br><br>
<label for="year">Publication Year:</label>
<input type="number" id="year" name="year" value="<?php echo $book['year']; ?>" required><br><br>
<button type="submit" name="update" class="submit-btn">Update Book</button>
</form>
</div>
</body>
</html>
<?php
} else {
echo "No book found with ID: $bookId";
}
$stmt->close();
}
// Check if the form is submitted to update book details
if (isset($_POST['update'])) {
// Get the updated book details from the form
$id = $_POST['id'];
$title = $_POST['title'];
$author = $_POST['author'];
$genre = $_POST['genre'];
$year = $_POST['year'];
// Update the book in the database
$stmt = $conn->prepare("UPDATE books SET title = ?, author = ?, genre = ?, year = ? WHERE id = ?");
$stmt->bind_param("sssii", $title, $author, $genre, $year, $id);
if ($stmt->execute()) {
echo "Book updated successfully!";
} else {
echo "Error updating book: " . $stmt->error;
}
$stmt->close();
}
// Close connection
$conn->close();
?>