用PHP编写一个图书管理网站
时间: 2023-08-21 07:03:26 浏览: 256
好的,下面是一个简单的图书管理网站的代码示例,使用PHP和MySQL数据库。
1. 创建数据库和表
首先,创建一个名为`library`的数据库,并创建一个名为`books`的表,包含以下字段:
- `id`:自增长的图书ID
- `title`:图书标题
- `author`:作者名字
- `description`:图书描述
- `image`:图书封面图片的URL
- `created_at`:图书添加到数据库的时间戳
使用以下SQL代码可以创建`library`数据库和`books`表:
```
CREATE DATABASE library;
USE library;
CREATE TABLE books (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author VARCHAR(255) NOT NULL,
description TEXT,
image VARCHAR(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```
2. 创建网站页面
创建以下文件:
- `index.php`:显示所有图书的列表
- `add_book.php`:添加新图书
- `edit_book.php`:编辑现有图书
- `delete_book.php`:删除现有图书
3. 编写PHP代码
a. index.php
```
<?php
// 连接数据库
$mysqli = new mysqli("localhost", "username", "password", "library");
// 检查连接是否成功
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
// 查询所有图书
$result = $mysqli->query("SELECT * FROM books");
// 显示所有图书
while ($row = $result->fetch_assoc()) {
echo "<h2>{$row['title']}</h2>";
echo "<p>作者:{$row['author']}</p>";
echo "<p>{$row['description']}</p>";
echo "<img src='{$row['image']}' alt='{$row['title']}'><br>";
echo "<a href='edit_book.php?id={$row['id']}'>编辑</a> | ";
echo "<a href='delete_book.php?id={$row['id']}' onclick='return confirm(\"确定删除吗?\")'>删除</a><hr>";
}
// 释放结果集和关闭数据库连接
$result->free();
$mysqli->close();
?>
```
b. add_book.php
```
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// 处理表单提交
$title = $_POST["title"];
$author = $_POST["author"];
$description = $_POST["description"];
$image = $_POST["image"];
// 连接数据库
$mysqli = new mysqli("localhost", "username", "password", "library");
// 检查连接是否成功
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
// 插入新图书
$result = $mysqli->query("INSERT INTO books (title, author, description, image) VALUES ('$title', '$author', '$description', '$image')");
// 关闭数据库连接
$mysqli->close();
// 重定向到首页
header("Location: index.php");
exit();
}
?>
<h2>添加新图书</h2>
<form method="post">
<label for="title">标题:</label>
<input type="text" name="title" required><br>
<label for="author">作者:</label>
<input type="text" name="author" required><br>
<label for="description">描述:</label>
<textarea name="description"></textarea><br>
<label for="image">封面图片的URL:</label>
<input type="text" name="image"><br>
<input type="submit" value="添加">
</form>
```
c. edit_book.php
```
<?php
// 获取要编辑的图书ID
$id = $_GET["id"];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// 处理表单提交
$title = $_POST["title"];
$author = $_POST["author"];
$description = $_POST["description"];
$image = $_POST["image"];
// 连接数据库
$mysqli = new mysqli("localhost", "username", "password", "library");
// 检查连接是否成功
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
// 更新图书
$result = $mysqli->query("UPDATE books SET title='$title', author='$author', description='$description', image='$image' WHERE id=$id");
// 关闭数据库连接
$mysqli->close();
// 重定向到首页
header("Location: index.php");
exit();
}
// 连接数据库
$mysqli = new mysqli("localhost", "username", "password", "library");
// 检查连接是否成功
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
// 查询要编辑的图书
$result = $mysqli->query("SELECT * FROM books WHERE id=$id");
// 显示图书编辑表单
if ($row = $result->fetch_assoc()) {
?>
<h2>编辑图书</h2>
<form method="post">
<label for="title">标题:</label>
<input type="text" name="title" value="<?php echo $row['title']; ?>" required><br>
<label for="author">作者:</label>
<input type="text" name="author" value="<?php echo $row['author']; ?>" required><br>
<label for="description">描述:</label>
<textarea name="description"><?php echo $row['description']; ?></textarea><br>
<label for="image">封面图片的URL:</label>
<input type="text" name="image" value="<?php echo $row['image']; ?>"><br>
<input type="submit" value="保存">
</form>
<?php
}
// 释放结果集和关闭数据库连接
$result->free();
$mysqli->close();
?>
```
d. delete_book.php
```
<?php
// 获取要删除的图书ID
$id = $_GET["id"];
// 连接数据库
$mysqli = new mysqli("localhost", "username", "password", "library");
// 检查连接是否成功
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
// 删除图书
$result = $mysqli->query("DELETE FROM books WHERE id=$id");
// 关闭数据库连接
$mysqli->close();
// 重定向到首页
header("Location: index.php");
exit();
?>
```
4. 运行网站
将上述PHP文件放在网站根目录下,并在浏览器中访问`http://localhost/index.php`即可查看所有图书的列表。
点击“添加新图书”链接,即可添加新图书。
点击“编辑”链接,即可编辑现有图书。
点击“删除”链接,即可删除现有图书。
阅读全文