书管理系统代码php

Below is a simple example of a book management system implemented in PHP. This system allows users to add, delete, and display books.

php
<!DOCTYPE html> <html> <head> <title>Book Management System</title> </head> <body> <h2>Book Management System</h2> <?php // Database connection $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "book_management"; $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Add book if (isset($_POST['add'])) { $title = $_POST['title']; $author = $_POST['author']; $sql = "INSERT INTO books (title, author) VALUES ('$title', '$author')"; if ($conn->query($sql) === TRUE) { echo "Book added successfully."; } else { echo "Error: " . $sql . "<br>" . $conn->error; } } // Delete book if (isset($_GET['delete'])) { $id = $_GET['delete']; $sql = "DELETE FROM books WHERE id=$id"; if ($conn->query($sql) === TRUE) { echo "Book deleted successfully."; } else { echo "Error deleting record: " . $conn->error; } } // Display books $sql = "SELECT * FROM books"; $result = $conn->query($sql); if ($result->num_rows > 0) { echo "<table border='1'>"; echo "<tr><th>ID</th><th>Title</th><th>Author</th><th>Action</th></tr>"; while($row = $result->fetch_assoc()) { echo "<tr>"; echo "<td>".$row["id"]."</td>"; echo "<td>".$row["title"]."</td>"; echo "<td>".$row["author"]."</td>"; echo "<td><a href='?delete=".$row["id"]."'>Delete</a></td>"; echo "</tr>"; } echo "</table>"; } else { echo "0 results"; } $conn->close(); ?> <h3>Add Book</h3> <form method="post" action=""> Title: <input type="text" name="title"><br> Author: <input type="text" name="author"><br> <input type="submit" name="add" value="Add Book"> </form> </body> </html>

This code provides a simple web interface where you can add books by filling out a form and delete books by clicking on a delete link next to each book entry. The book information is stored in a MySQL database named book_management. Make sure to replace username and password with your database credentials.

Sure, let's continue with the PHP code for the book management system. In the previous code snippet, we handled adding, deleting, and displaying books. Now, let's add the functionality to edit books.

php
<!DOCTYPE html> <html> <head> <title>Book Management System</title> </head> <body> <h2>Book Management System</h2> <?php // Database connection $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "book_management"; $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Add book if (isset($_POST['add'])) { $title = $_POST['title']; $author = $_POST['author']; $sql = "INSERT INTO books (title, author) VALUES ('$title', '$author')"; if ($conn->query($sql) === TRUE) { echo "Book added successfully."; } else { echo "Error: " . $sql . "<br>" . $conn->error; } } // Delete book if (isset($_GET['delete'])) { $id = $_GET['delete']; $sql = "DELETE FROM books WHERE id=$id"; if ($conn->query($sql) === TRUE) { echo "Book deleted successfully."; } else { echo "Error deleting record: " . $conn->error; } } // Edit book if (isset($_POST['edit'])) { $id = $_POST['id']; $title = $_POST['title']; $author = $_POST['author']; $sql = "UPDATE books SET title='$title', author='$author' WHERE id=$id"; if ($conn->query($sql) === TRUE) { echo "Book updated successfully."; } else { echo "Error updating record: " . $conn->error; } } // Display books $sql = "SELECT * FROM books"; $result = $conn->query($sql); if ($result->num_rows > 0) { echo "<table border='1'>"; echo "<tr><th>ID</th><th>Title</th><th>Author</th><th>Action</th></tr>"; while($row = $result->fetch_assoc()) { echo "<tr>"; echo "<td>".$row["id"]."</td>"; echo "<td>".$row["title"]."</td>"; echo "<td>".$row["author"]."</td>"; echo "<td> <a href='?delete=".$row["id"]."'>Delete</a> | <a href='edit.php?id=".$row["id"]."'>Edit</a> </td>"; echo "</tr>"; } echo "</table>"; } else { echo "0 results"; } $conn->close(); ?> <h3>Add Book</h3> <form method="post" action=""> Title: <input type="text" name="title"><br> Author: <input type="text" name="author"><br> <input type="submit" name="add" value="Add Book"> </form> </body> </html>

In this updated code, we added the functionality to edit books. When a user clicks on the "Edit" link next to a book, they will be directed to an edit.php page where they can modify the book's title and author. After making changes and submitting the form, the book will be updated in the database.