The only way I
<?php $conn = new mysqli("localhost", "root", "", "library"); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
$message = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $serial = $_POST["serial"]; $book = $_POST["book"]; $name = $_POST["name"]; $email = $_POST["email"]; $phone = $_POST["phone"];
$check = $conn->prepare("SELECT * FROM books WHERE serial_number = ?");
$check->bind_param("i", $serial);
$check->execute();
$result = $check->get_result();
if ($result->num_rows > 0) {
$message = "<p style='color:red;'>This book is already borrowed.</p>";
} else {
$stmt = $conn->prepare("INSERT INTO books (serial_number, book_name, borrower_name, borrower_email, borrower_phone) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("issss", $serial, $book, $name, $email, $phone);
$stmt->execute();
$message = "<p style='color:green;'>Book added successfully.</p>";
$stmt->close();
}
$check->close();
} ?>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Book Borrowing</title> <!-- CSS IS GIVEN IN EXAM, DON'T WRITE THIS --> </head> <body>
<header> <img src="read.png" alt="READ logo"> </header>
<img src="books.jpg" alt="Books">
<form method="POST" action="q2.php"> <table> <tr> <td>Book Serial Number:</td> <td><input type="number" name="serial" required></td> </tr> <tr> <td>Book Name:</td> <td><input type="text" name="book" required></td> </tr> <tr> <td>Borrower Name:</td> <td><input type="text" name="name" required></td> </tr> <tr> <td>Borrower Email:</td> <td><input type="text" name="email" required></td> </tr> <tr> <td>Borrower Phone:</td> <td><input type="text" name="phone" required></td> </tr> </table> <button type="submit">Submit</button> </form>
<?php echo $message; ?>
<?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $all = $conn->query("SELECT * FROM books"); echo "<table border='1'>"; echo "<tr><th>Serial</th><th>Book</th><th>Borrower</th><th>Email</th><th>Phone</th></tr>"; while ($row = $all->fetch_assoc()) { echo "<tr> <td>" . htmlspecialchars($row["serial_number"]) . "</td> <td>" . htmlspecialchars($row["book_name"]) . "</td> <td>" . htmlspecialchars($row["borrower_name"]) . "</td> <td>" . htmlspecialchars($row["borrower_email"]) . "</td> <td>" . htmlspecialchars($row["borrower_phone"]) . "</td> </tr>"; } echo "</table>"; } $conn->close(); ?>
</body> </html>