用PHP写一个图书管理系统,要求1:有用户登录界面2:有新增内容按钮;3:能够实时更新图书资源
时间: 2024-01-21 10:16:49 浏览: 21
好的,以下是一个简单的图书管理系统的示例代码:
index.php
```php
<?php
session_start();
if(!isset($_SESSION['username'])) { // 如果未登录
header("Location: login.php"); // 跳转到登录页面
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>图书管理系统</title>
</head>
<body>
<h1>欢迎来到图书管理系统!</h1>
<h3>当前登录用户:<?php echo $_SESSION['username']; ?></h3>
<br>
<a href="add_book.php">新增图书</a>
<br>
<br>
<table>
<tr>
<th>图书编号</th>
<th>图书名称</th>
<th>图书作者</th>
<th>图书出版社</th>
<th>图书价格</th>
</tr>
<?php
// 连接数据库
$conn = mysqli_connect("localhost", "root", "", "library");
// 查询图书信息
$result = mysqli_query($conn, "SELECT * FROM books");
// 遍历结果集并输出表格行
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['author']."</td>";
echo "<td>".$row['publisher']."</td>";
echo "<td>".$row['price']."</td>";
echo "</tr>";
}
// 关闭数据库连接
mysqli_close($conn);
?>
</table>
</body>
</html>
```
login.php
```php
<?php
session_start();
if(isset($_POST['username']) && isset($_POST['password'])) { // 如果提交了登录表单
// 连接数据库
$conn = mysqli_connect("localhost", "root", "", "library");
// 查询用户信息
$result = mysqli_query($conn, "SELECT * FROM users WHERE username='".$_POST['username']."' AND password='".$_POST['password']."'");
if(mysqli_num_rows($result) > 0) { // 如果查询到了该用户
$_SESSION['username'] = $_POST['username']; // 设置 session 变量
header("Location: index.php"); // 跳转到首页
exit();
} else { // 如果未查询到该用户
$error_message = "用户名或密码错误!";
}
// 关闭数据库连接
mysqli_close($conn);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>登录图书管理系统</title>
</head>
<body>
<h1>登录图书管理系统</h1>
<?php
if(isset($error_message)) {
echo "<p style='color:red;'>".$error_message."</p>";
}
?>
<form method="POST">
<label>用户名:</label>
<input type="text" name="username">
<br>
<label>密码:</label>
<input type="password" name="password">
<br>
<input type="submit" value="登录">
</form>
</body>
</html>
```
add_book.php
```php
<?php
session_start();
if(!isset($_SESSION['username'])) { // 如果未登录
header("Location: login.php"); // 跳转到登录页面
exit();
}
if(isset($_POST['name']) && isset($_POST['author']) && isset($_POST['publisher']) && isset($_POST['price'])) { // 如果提交了新增图书表单
// 连接数据库
$conn = mysqli_connect("localhost", "root", "", "library");
// 插入图书信息
mysqli_query($conn, "INSERT INTO books (name, author, publisher, price) VALUES ('".$_POST['name']."', '".$_POST['author']."', '".$_POST['publisher']."', ".$_POST['price'].")");
// 关闭数据库连接
mysqli_close($conn);
header("Location: index.php"); // 跳转到首页
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>新增图书</title>
</head>
<body>
<h1>新增图书</h1>
<form method="POST">
<label>图书名称:</label>
<input type="text" name="name">
<br>
<label>图书作者:</label>
<input type="text" name="author">
<br>
<label>图书出版社:</label>
<input type="text" name="publisher">
<br>
<label>图书价格:</label>
<input type="text" name="price">
<br>
<input type="submit" value="新增">
</form>
</body>
</html>
```
以上代码实现了一个简单的图书管理系统,包括登录、新增图书和实时更新图书资源等功能。需要注意的是,本示例代码未使用任何框架,仅为演示用途,实际应用中建议使用框架来提高开发效率和代码质量。