基于web的图书管理系统代码
时间: 2023-12-15 10:03:55 浏览: 26
抱歉,我是一个语言模型AI,无法提供完整的代码。以下是一个基于PHP和MySQL的图书管理系统的简单示例:
index.php
```
<!DOCTYPE html>
<html>
<head>
<title>图书管理系统</title>
</head>
<body>
<h1>图书管理系统</h1>
<a href="booklist.php">图书列表</a>
<a href="addbook.php">添加图书</a>
</body>
</html>
```
booklist.php
```
<?php
//连接数据库
$conn = mysqli_connect("localhost", "username", "password", "database");
//检查连接
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
//查询图书列表
$sql = "SELECT * FROM books";
$result = mysqli_query($conn, $sql);
//输出图书列表
if (mysqli_num_rows($result) > 0) {
echo "<table><tr><th>ID</th><th>书名</th><th>作者</th><th>出版社</th></tr>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>".$row["id"]."</td><td>".$row["title"]."</td><td>".$row["author"]."</td><td>".$row["publisher"]."</td></tr>";
}
echo "</table>";
} else {
echo "没有找到任何记录";
}
//关闭连接
mysqli_close($conn);
?>
```
addbook.php
```
<!DOCTYPE html>
<html>
<head>
<title>添加图书</title>
</head>
<body>
<h1>添加图书</h1>
<form action="savebook.php" method="post">
<label for="title">书名:</label>
<input type="text" id="title" name="title"><br><br>
<label for="author">作者:</label>
<input type="text" id="author" name="author"><br><br>
<label for="publisher">出版社:</label>
<input type="text" id="publisher" name="publisher"><br><br>
<input type="submit" value="提交">
</form>
</body>
</html>
```
savebook.php
```
<?php
//连接数据库
$conn = mysqli_connect("localhost", "username", "password", "database");
//检查连接
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
//获取表单数据
$title = $_POST["title"];
$author = $_POST["author"];
$publisher = $_POST["publisher"];
//插入数据
$sql = "INSERT INTO books (title, author, publisher) VALUES ('$title', '$author', '$publisher')";
if (mysqli_query($conn, $sql)) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
//关闭连接
mysqli_close($conn);
?>
```
以上仅仅是一个简单的示例,实际的图书管理系统需要更多的功能和复杂的逻辑。