php实现图书管理系统的增删改查
时间: 2024-01-03 17:04:50 浏览: 135
以下是使用PHP实现图书管理系统的增删改查的示例代码:
1. 添加图书:
```php
<?php
// 连接数据库
$conn = mysqli_connect("localhost", "username", "password", "database");
// 获取表单数据
$bookName = $_POST['bookName'];
$author = $_POST['author'];
$price = $_POST['price'];
// 插入数据
$sql = "INSERT INTO books (bookName, author, price) VALUES ('$bookName', '$author', '$price')";
mysqli_query($conn, $sql);
// 关闭数据库连接
mysqli_close($conn);
?>
```
2. 删除图书:
```php
<?php
// 连接数据库
$conn = mysqli_connect("localhost", "username", "password", "database");
// 获取要删除的图书ID
$bookId = $_GET['bookId'];
// 删除数据
$sql = "DELETE FROM books WHERE bookId = $bookId";
mysqli_query($conn, $sql);
// 关闭数据库连接
mysqli_close($conn);
?>
```
3. 修改图书:
```php
<?php
// 连接数据库
$conn = mysqli_connect("localhost", "username", "password", "database");
// 获取表单数据
$bookId = $_POST['bookId'];
$bookName = $_POST['bookName'];
$author = $_POST['author'];
$price = $_POST['price'];
// 更新数据
$sql = "UPDATE books SET bookName = '$bookName', author = '$author', price = '$price' WHERE bookId = $bookId";
mysqli_query($conn, $sql);
// 关闭数据库连接
mysqli_close($conn);
?>
```
4. 查询图书:
```php
<?php
// 连接数据库
$conn = mysqli_connect("localhost", "username", "password", "database");
// 查询数据
$sql = "SELECT * FROM books";
$result = mysqli_query($conn, $sql);
// 输出查询结果
while ($row = mysqli_fetch_assoc($result)) {
echo "书名:" . $row['bookName'] . "<br>";
echo "作者:" . $row['author'] . "<br>";
echo "价格:" . $row['price'] . "<br><br>";
}
// 关闭数据库连接
mysqli_close($conn);
?>
```
阅读全文