帮我写一套php图书管理系统
时间: 2023-08-10 17:08:53 浏览: 114
php图书管理系统
5星 · 资源好评率100%
好的,下面是一个简单的 PHP 图书管理系统的实现步骤:
1. 创建数据库并设计数据表结构。
创建一个名为 “book_management_system” 的数据库,并创建以下 2 张数据表:
books:包含书籍的信息,如书籍 ID、书名、作者、出版社、出版日期、价格等。
users:包含用户的信息,如用户 ID、用户名、密码、邮箱等。
2. 编写连接数据库的 PHP 文件。
在根目录下创建一个名为 “connection.php” 的文件,用于连接数据库并初始化数据库连接对象。
```php
<?php
$host = "localhost";
$username = "root";
$password = "";
$dbname = "book_management_system";
$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
```
3. 编写书籍信息的 CRUD 操作。
在根目录下创建一个名为 “book.php” 的文件,用于实现书籍信息的 CRUD 操作。具体实现如下:
- 添加书籍信息
```php
function addBook($bookName, $author, $publisher, $publishDate, $price) {
global $conn;
$sql = "INSERT INTO books (book_name, author, publisher, publish_date, price)
VALUES ('$bookName', '$author', '$publisher', '$publishDate', '$price')";
if ($conn->query($sql) === TRUE) {
return true;
} else {
return false;
}
}
```
- 更新书籍信息
```php
function updateBook($bookId, $bookName, $author, $publisher, $publishDate, $price) {
global $conn;
$sql = "UPDATE books SET book_name = '$bookName', author = '$author', publisher = '$publisher', publish_date = '$publishDate', price = '$price' WHERE book_id = '$bookId'";
if ($conn->query($sql) === TRUE) {
return true;
} else {
return false;
}
}
```
- 删除书籍信息
```php
function deleteBook($bookId) {
global $conn;
$sql = "DELETE FROM books WHERE book_id = '$bookId'";
if ($conn->query($sql) === TRUE) {
return true;
} else {
return false;
}
}
```
- 获取书籍信息
```php
function getBook($bookId) {
global $conn;
$sql = "SELECT * FROM books WHERE book_id = '$bookId'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
return $result->fetch_assoc();
} else {
return false;
}
}
```
- 获取所有书籍信息
```php
function getAllBooks() {
global $conn;
$sql = "SELECT * FROM books";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$books = array();
while ($row = $result->fetch_assoc()) {
$books[] = $row;
}
return $books;
} else {
return false;
}
}
```
4. 编写用户信息的 CRUD 操作。
在根目录下创建一个名为 “user.php” 的文件,用于实现用户信息的 CRUD 操作。具体实现如下:
- 添加用户信息
```php
function addUser($username, $password, $email) {
global $conn;
$sql = "INSERT INTO users (username, password, email)
VALUES ('$username', '$password', '$email')";
if ($conn->query($sql) === TRUE) {
return true;
} else {
return false;
}
}
```
- 更新用户信息
```php
function updateUser($userId, $username, $password, $email) {
global $conn;
$sql = "UPDATE users SET username = '$username', password = '$password', email = '$email' WHERE user_id = '$userId'";
if ($conn->query($sql) === TRUE) {
return true;
} else {
return false;
}
}
```
- 删除用户信息
```php
function deleteUser($userId) {
global $conn;
$sql = "DELETE FROM users WHERE user_id = '$userId'";
if ($conn->query($sql) === TRUE) {
return true;
} else {
return false;
}
}
```
- 获取用户信息
```php
function getUser($userId) {
global $conn;
$sql = "SELECT * FROM users WHERE user_id = '$userId'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
return $result->fetch_assoc();
} else {
return false;
}
}
```
- 获取所有用户信息
```php
function getAllUsers() {
global $conn;
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$users = array();
while ($row = $result->fetch_assoc()) {
$users[] = $row;
}
return $users;
} else {
return false;
}
}
```
5. 编写前端页面。
在根目录下创建一个名为 “index.php” 的文件,用于实现前端页面。具体实现如下:
```php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书管理系统</title>
</head>
<body>
<h1>图书管理系统</h1>
<h2>添加书籍</h2>
<form action="add_book.php" method="POST">
<label for="book_name">书名:</label>
<input type="text" id="book_name" name="book_name"><br>
<label for="author">作者:</label>
<input type="text" id="author" name="author"><br>
<label for="publisher">出版社:</label>
<input type="text" id="publisher" name="publisher"><br>
<label for="publish_date">出版日期:</label>
<input type="date" id="publish_date" name="publish_date"><br>
<label for="price">价格:</label>
<input type="number" id="price" name="price"><br>
<input type="submit" value="添加">
</form>
<h2>书籍列表</h2>
<table>
<thead>
<tr>
<th>ID</th>
<th>书名</th>
<th>作者</th>
<th>出版社</th>
<th>出版日期</th>
<th>价格</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php
include_once 'book.php';
$books = getAllBooks();
if ($books) {
foreach ($books as $book) {
echo "<tr>";
echo "<td>" . $book['book_id'] . "</td>";
echo "<td>" . $book['book_name'] . "</td>";
echo "<td>" . $book['author'] . "</td>";
echo "<td>" . $book['publisher'] . "</td>";
echo "<td>" . $book['publish_date'] . "</td>";
echo "<td>" . $book['price'] . "</td>";
echo "<td>";
echo "<a href='edit_book.php?id=" . $book['book_id'] . "'>编辑</a>";
echo "<a href='delete_book.php?id=" . $book['book_id'] . "' style='margin-left: 10px;'>删除</a>";
echo "</td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='7' style='text-align: center;'>没有数据</td></tr>";
}
?>
</tbody>
</table>
</body>
</html>
```
6. 编写添加书籍信息的页面。
在根目录下创建一个名为 “add_book.php” 的文件,用于实现添加书籍信息的页面。具体实现如下:
```php
<?php
include_once 'book.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$bookName = $_POST['book_name'];
$author = $_POST['author'];
$publisher = $_POST['publisher'];
$publishDate = $_POST['publish_date'];
$price = $_POST['price'];
if (addBook($bookName, $author, $publisher, $publishDate, $price)) {
header("Location: index.php");
exit();
} else {
echo "添加书籍信息失败!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加书籍信息</title>
</head>
<body>
<h1>添加书籍信息</h1>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
<label for="book_name">书名:</label>
<input type="text" id="book_name" name="book_name"><br>
<label for="author">作者:</label>
<input type="text" id="author" name="author"><br>
<label for="publisher">出版社:</label>
<input type="text" id="publisher" name="publisher"><br>
<label for="publish_date">出版日期:</label>
<input type="date" id="publish_date" name="publish_date"><br>
<label for="price">价格:</label>
<input type="number" id="price" name="price"><br>
<input type="submit" value="添加">
</form>
</body>
</html>
```
7. 编写编辑书籍信息的页面。
在根目录下创建一个名为 “edit_book.php” 的文件,用于实现编辑书籍信息的页面。具体实现如下:
```php
<?php
include_once 'book.php';
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$bookId = $_GET['id'];
$book = getBook($bookId);
if ($book) {
$bookName = $book['book_name'];
$author = $book['author'];
$publisher = $book['publisher'];
$publishDate = $book['publish_date'];
$price = $book['price'];
} else {
echo "获取书籍信息失败!";
exit();
}
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
$bookId = $_POST['book_id'];
$bookName = $_POST['book_name'];
$author = $_POST['author'];
$publisher = $_POST['publisher'];
$publishDate = $_POST['publish_date'];
$price = $_POST['price'];
if (updateBook($bookId, $bookName, $author, $publisher, $publishDate, $price)) {
header("Location: index.php");
exit();
} else {
echo "更新书籍信息失败!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>编辑书籍信息</title>
</head>
<body>
<h1>编辑书籍信息</h1>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
<input type="hidden" name="book_id" value="<?php echo $bookId; ?>">
<label for="book_name">书名:</label>
<input type="text" id="book_name" name="book_name" value="<?php echo $bookName; ?>"><br>
<label for="author">作者:</label>
<input type="text" id="author" name="author" value="<?php echo $author; ?>"><br>
<label for="publisher">出版社:</label>
<input type="text" id="publisher" name="publisher" value="<?php echo $publisher; ?>"><br>
<label for="publish_date">出版日期:</label>
<input type="date" id="publish_date" name="publish_date" value="<?php echo $publishDate; ?>"><br>
<label for="price">价格:</label>
<input type="number" id="price" name="price" value="<?php echo $price; ?>"><br>
<input type="submit" value="保存">
</form>
</body>
</html>
```
8. 编写删除书籍信息的页面。
在根目录下创建一个名为 “delete_book.php” 的文件,用于实现删除书籍信息的页面。具体实现如下:
```php
<?php
include_once 'book.php';
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$bookId = $_GET['id'];
if (deleteBook($bookId)) {
header("Location: index.php");
exit();
} else {
echo "删除书籍信息失败!";
}
}
?>
```
以上就是一个简单的 PHP 图书管理系统的实现步骤。当然,这只是一个基础版本,您可以根据自己的需求进行功能扩展和优化。
阅读全文