书管理系统php代码
Below is a simple example of a book management system implemented in PHP. This system allows users to add, delete, edit, and view books stored in a MySQL database.
First, you need to set up a MySQL database with a table named books
containing columns for id
, title
, author
, and year
. Here's the SQL to create such a table:
sqlCREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author VARCHAR(255) NOT NULL,
year INT NOT NULL
);
Then, you can use the following PHP code for the book management system:
php<?php
// Database connection
$servername = "localhost";
$username = "username"; // your MySQL username
$password = "password"; // your MySQL password
$database = "dbname"; // your MySQL database name
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Add a book
if (isset($_POST['add'])) {
$title = $_POST['title'];
$author = $_POST['author'];
$year = $_POST['year'];
$sql = "INSERT INTO books (title, author, year) VALUES ('$title', '$author', '$year')";
if ($conn->query($sql) === TRUE) {
echo "New book added successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
// Delete a 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) {
while ($row = $result->fetch_assoc()) {
echo "ID: " . $row['id'] . " - Title: " . $row['title'] . " - Author: " . $row['author'] . " - Year: " . $row['year'] . "<br>";
echo "<a href='?delete=" . $row['id'] . "'>Delete</a><br><br>";
}
} else {
echo "0 results";
}
// Close connection
$conn->close();
?>
<!-- HTML form to add a new book -->
<form method="post">
<input type="text" name="title" placeholder="Title" required><br>
<input type="text" name="author" placeholder="Author" required><br>
<input type="number" name="year" placeholder="Year" required><br>
<button type="submit" name="add">Add Book</button>
</form>
This code performs basic CRUD (Create, Read, Update, Delete) operations on the books
table. It displays all books in the database, allows users to add new books via a form, and provides a delete link for each book.
Sure, let's continue by adding functionality to edit existing books and handle update operations. Below is the modified PHP code:
php<?php
// Database connection
$servername = "localhost";
$username = "username"; // your MySQL username
$password = "password"; // your MySQL password
$database = "dbname"; // your MySQL database name
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Add a book
if (isset($_POST['add'])) {
$title = $_POST['title'];
$author = $_POST['author'];
$year = $_POST['year'];
$sql = "INSERT INTO books (title, author, year) VALUES ('$title', '$author', '$year')";
if ($conn->query($sql) === TRUE) {
echo "New book added successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
// Delete a 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 a book
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$sql = "SELECT * FROM books WHERE id=$id";
$result = $conn->query($sql);
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
$title = $row['title'];
$author = $row['author'];
$year = $row['year'];
} else {
echo "Book not found";
}
}
// Update a book
if (isset($_POST['update'])) {
$id = $_POST['id'];
$title = $_POST['title'];
$author = $_POST['author'];
$year = $_POST['year'];
$sql = "UPDATE books SET title='$title', author='$author', year='$year' 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) {
while ($row = $result->fetch_assoc()) {
echo "ID: " . $row['id'] . " - Title: " . $row['title'] . " - Author: " . $row['author'] . " - Year: " . $row['year'] . "<br>";
echo "<a href='?delete=" . $row['id'] . "'>Delete</a> | ";
echo "<a href='?edit=" . $row['id'] . "'>Edit</a><br><br>";
}
} else {
echo "0 results";
}
// Close connection
$conn->close();
?>
<!-- HTML form to add or update a book -->
<form method="post">
<?php if (isset($id)) { ?>
<input type="hidden" name="id" value="<?php echo $id; ?>">
<?php } ?>
<input type="text" name="title" placeholder="Title" value="<?php echo isset($title) ? $title : ''; ?>" required><br>
<input type="text" name="author" placeholder="Author" value="<?php echo isset($author) ? $author : ''; ?>" required><br>
<input type="number" name="year" placeholder="Year" value="<?php echo isset($year) ? $year : ''; ?>" required><br>
<button type="submit" name="<?php echo isset($id) ? 'update' : 'add'; ?>"><?php echo isset($id) ? 'Update' : 'Add'; ?> Book</button>
</form>
In this updated code, I've added functionality to edit existing books. When a user clicks on the "Edit" link for a specific book, the book's details are fetched from the database and populated into the form fields. The user can then modify the details and submit the form to update the book's information in the database.
Note: This code doesn't include input validation and sanitation, which is crucial for real-world applications to prevent SQL injection and other security vulnerabilities. Additionally, consider using prepared statements for database interactions to enhance security further.