u/Illustrious_Squash99

▲ 0 r/mysql

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-&gt;prepare("SELECT * FROM books WHERE serial_number = ?");
$check-&gt;bind_param("i", $serial);
$check-&gt;execute();
$result = $check-&gt;get_result();

if ($result-&gt;num_rows &gt; 0) {
    $message = "&lt;p style='color:red;'&gt;This book is already borrowed.&lt;/p&gt;";
} else {
    $stmt = $conn-&gt;prepare("INSERT INTO books (serial_number, book_name, borrower_name, borrower_email, borrower_phone) VALUES (?, ?, ?, ?, ?)");
    $stmt-&gt;bind_param("issss", $serial, $book, $name, $email, $phone);
    $stmt-&gt;execute();
    $message = "&lt;p style='color:green;'&gt;Book added successfully.&lt;/p&gt;";
    $stmt-&gt;close();
}
$check-&gt;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>

reddit.com
u/Illustrious_Squash99 — 3 days ago