用PHP+MySQL做一个图书管理系统并实现对数据库的增删等功能代码
时间: 2024-03-10 13:46:07 浏览: 133
好的,以下是一个简单的图书管理系统的实现示例:
1. 首先,需要创建一个MySQL数据库,其中包含两个表:book和category。book表用于存储图书信息,category表用于存储图书分类信息。具体的表结构如下:
book表:
| Field | Type | Null | Key | Default | Extra |
|-------------|--------------|------|-----|---------|----------------|
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(255) | NO | | NULL | |
| author | varchar(255) | NO | | NULL | |
| category_id | int(11) | NO | | NULL | |
| price | decimal(10,2)| NO | | NULL | |
category表:
| Field | Type | Null | Key | Default | Extra |
|-------|--------------|------|-----|---------|----------------|
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
2. 创建一个PHP文件,用于连接数据库,获取和更新数据。
```php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 获取图书列表
function get_books() {
global $conn;
$sql = "SELECT b.id, b.title, b.author, c.name as category_name, b.price FROM book b INNER JOIN category c ON b.category_id = c.id";
$result = $conn->query($sql);
$books = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$books[] = $row;
}
}
return $books;
}
// 获取分类列表
function get_categories() {
global $conn;
$sql = "SELECT * FROM category";
$result = $conn->query($sql);
$categories = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$categories[] = $row;
}
}
return $categories;
}
// 添加图书
function add_book($title, $author, $category_id, $price) {
global $conn;
$sql = "INSERT INTO book (title, author, category_id, price)
VALUES ('$title', '$author', '$category_id', '$price')";
if ($conn->query($sql) === TRUE) {
return true;
} else {
return false;
}
}
// 删除图书
function delete_book($book_id) {
global $conn;
$sql = "DELETE FROM book WHERE id='$book_id'";
if ($conn->query($sql) === TRUE) {
return true;
} else {
return false;
}
}
// 关闭连接
$conn->close();
?>
```
3. 创建一个页面,用于显示图书列表和添加图书。
```php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>图书管理系统</title>
</head>
<body>
<h1>图书列表</h1>
<table>
<tr>
<th>编号</th>
<th>标题</th>
<th>作者</th>
<th>分类</th>
<th>价格</th>
<th>操作</th>
</tr>
<?php
require_once 'db.php';
$books = get_books();
foreach ($books as $book) {
echo "<tr>";
echo "<td>".$book['id']."</td>";
echo "<td>".$book['title']."</td>";
echo "<td>".$book['author']."</td>";
echo "<td>".$book['category_name']."</td>";
echo "<td>".$book['price']."</td>";
echo "<td><a href=\"delete_book.php?id=".$book['id']."\">删除</a></td>";
echo "</tr>";
}
?>
</table>
<h1>添加图书</h1>
<form method="post" action="add_book.php">
<label>标题:</label><input type="text" name="title"><br>
<label>作者:</label><input type="text" name="author"><br>
<label>分类:</label>
<select name="category_id">
<?php
$categories = get_categories();
foreach ($categories as $category) {
echo "<option value=\"".$category['id']."\">".$category['name']."</option>";
}
?>
</select><br>
<label>价格:</label><input type="text" name="price"><br>
<input type="submit" value="添加">
</form>
</body>
</html>
```
4. 创建一个页面,用于处理添加图书的请求。
```php
<?php
require_once 'db.php';
$title = $_POST['title'];
$author = $_POST['author'];
$category_id = $_POST['category_id'];
$price = $_POST['price'];
if (add_book($title, $author, $category_id, $price)) {
echo "添加成功!";
} else {
echo "添加失败!";
}
?>
```
5. 创建一个页面,用于处理删除图书的请求。
```php
<?php
require_once 'db.php';
$book_id = $_GET['id'];
if (delete_book($book_id)) {
echo "删除成功!";
} else {
echo "删除失败!";
}
?>
```
注意:以上示例只是一个简单的演示,实际开发中需要考虑更多的因素,如安全性、性能等。
阅读全文