PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
<?php $host = 'localhost'; // Database host $user = 'root'; // MySQL username $password = ''; // MySQL password (default is empty in XAMPP) $dbname = 'library_db'; // Database name // Create a connection to the MySQL database try { $conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $password); // Set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?>
Copy Clear
Copy Format Clear
<?php // Database connection $host = 'localhost'; $user = 'root'; $password = ''; $dbname = 'library_db'; try { $conn = new PDB("mysql:host=$host;dbname=$dbname", $user, $password); $conn->setAttribute(PDB::ATTR_ERRMODE, PDB::ERRMODE_EXCEPTION); } catch (PDOException $e) { die("Connection failed: " . $e->getMessage()); } // Handle CRUD Operations // Add a new book if (isset($_POST['add_book'])) { $title = $_POST['title']; $isbn = $_POST['isbn']; $quantity = $_POST['quantity']; $publication_year = $_POST['publication_year']; $genre = $_POST['genre']; $query = "INSERT INTO Books (title, isbn, quantity, publication_year, genre) VALUES (?, ?, ?, ?, ?)"; $stmt = $conn->prepare($query); $stmt->execute([$title, $isbn, $quantity, $publication_year, $genre]); header("Location: index.php"); } // Update a book's information if (isset($_POST['update_book'])) { $book_id = $_POST['book_id']; $title = $_POST['title']; $isbn = $_POST['isbn']; $quantity = $_POST['quantity']; $publication_year = $_POST['publication_year']; $genre = $_POST['genre']; $query = "UPDATE Books SET title = ?, isbn = ?, quantity = ?, publication_year = ?, genre = ? WHERE book_id = ?"; $stmt = $conn->prepare($query); $stmt->execute([$title, $isbn, $quantity, $publication_year, $genre, $book_id]); header("Location: index.php"); } // Delete a book if (isset($_GET['delete_book'])) { $book_id = $_GET['delete_book']; $query = "DELETE FROM Books WHERE book_id = ?"; $stmt = $conn->prepare($query); $stmt->execute([$book_id]); header("Location: index.php"); } // Fetch all books $query = "SELECT * FROM Books"; $stmt = $conn->prepare($query); $stmt->execute(); $books = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Library Management System</title> <style> body { font-family: Arial, sans-serif; background-color: #f4f4f9; margin: 0; padding: 0; } header { background-color: #4CAF50; color: white; padding: 15px; text-align: center; } .container { width: 80%; margin: 20px auto; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } table, th, td { border: 1px solid #ddd; } th, td { padding: 10px; text-align: center; } .form-container { background-color: #fff; padding: 20px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); margin-top: 20px; } .form-container input { padding: 10px; margin: 10px 0; width: 100%; border-radius: 5px; border: 1px solid #ccc; } .form-container button { padding: 10px 20px; background-color: #4CAF50; color: white; border: none; border-radius: 5px; cursor: pointer; } .form-container button:hover { background-color: #45a049; } </style> </head> <body> <header> <h1>Library Management System</h1> </header> <div class="container"> <div class="form-container"> <h2>Add a New Book</h2> <form method="POST" action="index.php"> <input type="text" name="title" placeholder="Title" required> <input type="text" name="isbn" placeholder="ISBN" required> <input type="number" name="quantity" placeholder="Quantity" required> <input type="number" name="publication_year" placeholder="Publication Year" required> <input type="text" name="genre" placeholder="Genre" required> <button type="submit" name="add_book">Add Book</button> </form> </div> <div class="form-container"> <h2>Update Book Information</h2> <form method="POST" action="index.php"> <input type="number" name="book_id" placeholder="Book ID" required> <input type="text" name="title" placeholder="Title" required> <input type="text" name="isbn" placeholder="ISBN" required> <input type="number" name="quantity" placeholder="Quantity" required> <input type="number" name="publication_year" placeholder="Publication Year" required> <input type="text" name="genre" placeholder="Genre" required> <button type="submit" name="update_book">Update Book</button> </form> </div> <h2>Books in Library</h2> <table> <thead> <tr> <th>Book ID</th> <th>Title</th> <th>ISBN</th> <th>Quantity</th> <th>Publication Year</th> <th>Genre</th> <th>Actions</th> </tr> </thead> <tbody> <?php foreach ($books as $book): ?> <tr> <td><?= $book['book_id'] ?></td> <td><?= $book['title'] ?></td> <td><?= $book['isbn'] ?></td> <td><?= $book['quantity'] ?></td> <td><?= $book['publication_year'] ?></td> <td><?= $book['genre'] ?></td> <td> <a href="index.php?delete_book=<?= $book['book_id'] ?>" onclick="return confirm('Are you sure you want to delete this book?')">Delete</a> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> </body> </html>
Show:  
Copy Clear