教材管理系统是一个基于PHP和SQL的Web应用程序,旨在为学校提供一个高效的教材管理平台。该系统可以帮助管理员录入教材信息、教师查询和申请教材、学生查询教材信息,提高教材管理的效率和透明度。
用户管理
教材管理
申请管理
库存管理
系统设置
用户表(users)
id
(INT, 主键)username
(VARCHAR, 用户名)password
(VARCHAR, 密码)role
(VARCHAR, 角色)created_at
(DATETIME, 创建时间)updated_at
(DATETIME, 更新时间)教材表(textbooks)
id
(INT, 主键)title
(VARCHAR, 教材名称)author
(VARCHAR, 作者)publisher
(VARCHAR, 出版社)isbn
(VARCHAR, ISBN编号)quantity
(INT, 库存数量)created_at
(DATETIME, 创建时间)updated_at
(DATETIME, 更新时间)申请表(applications)
id
(INT, 主键)user_id
(INT, 外键,关联用户表)textbook_id
(INT, 外键,关联教材表)quantity
(INT, 申请数量)status
(VARCHAR, 申请状态)created_at
(DATETIME, 创建时间)updated_at
(DATETIME, 更新时间)表现层(Presentation Layer)
业务逻辑层(Business Logic Layer)
数据访问层(Data Access Layer)
控制器负责处理用户的请求,并调用相应的模型方法。示例如下:
session_start();
// 连接数据库
$host = 'localhost';
$db = 'textbook_management';
$user = 'root';
$pass = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Could not connect to the database $db :" . $e->getMessage());
}
// 用户登录
if (isset($_POST['login'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
$stmt->execute(['username' => $username, 'password' => $password]);
$user = $stmt->fetch();
if ($user) {
$_SESSION['user'] = $user;
header('Location: dashboard.php');
} else {
echo "Invalid username or password.";
}
}
?>
模型负责处理数据的存取操作。示例如下:
class Textbook {
private $pdo;
public function __construct($pdo) {
$this->pdo = $pdo;
}
public function getAllTextbooks() {
$stmt = $this->pdo->query("SELECT * FROM textbooks");
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function addTextbook($title, $author, $publisher, $isbn, $quantity) {
$stmt = $this->pdo->prepare("INSERT INTO textbooks (title, author, publisher, isbn, quantity) VALUES (:title, :author, :publisher, :isbn, :quantity)");
$stmt->execute(['title' => $title, 'author' => $author, 'publisher' => $publisher, 'isbn' => $isbn, 'quantity' => $quantity]);
}
public function getTextbookById($id) {
$stmt = $this->pdo->prepare("SELECT * FROM textbooks WHERE id = :id");
$stmt->execute(['id' => $id]);
return $stmt->fetch(PDO::FETCH_ASSOC);
}
public function updateTextbook($id, $title, $author, $publisher, $isbn, $quantity) {
$stmt = $this->pdo->prepare("UPDATE textbooks SET title = :title, author = :author, publisher = :publisher, isbn = :isbn, quantity = :quantity WHERE id = :id");
$stmt->execute(['id' => $id, 'title' => $title, 'author' => $author, 'publisher' => $publisher, 'isbn' => $isbn, 'quantity' => $quantity]);
}
public function deleteTextbook($id) {
$stmt = $this->pdo->prepare("DELETE FROM textbooks WHERE id = :id");
$stmt->execute(['id' => $id]);
}
}
?>
视图负责显示数据。示例如下:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>教材列表title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
head>
<body>
<div class="container">
<h1>教材列表h1>
<table class="table table-striped">
<thead>
<tr>
<th>教材名称th>
<th>作者th>
<th>出版社th>
<th>ISBN编号th>
<th>库存数量th>
<th>操作th>
tr>
thead>
<tbody>
<tr>
<td>td>
<td>td>
<td>td>
<td>td>
<td>td>
<td>
<a href="edit-textbook.php?id='id']; ?>" class="btn btn-primary">编辑a>
<a href="delete-textbook.php?id='id']; ?>" class="btn btn-danger">删除a>
td>
tr>
tbody>
table>
<a href="add-textbook.php" class="btn btn-success">添加教材a>
div>
body>
html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>注册</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>注册</h1>
<form action="register.php" method="post">
<div class="form-group">
<label for="username">用户名</label>
<input type="text" class="form-control" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">密码</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="form-group">
<label for="role">角色</label>
<select class="form-control" id="role" name="role" required>
<option value="admin">管理员</option>
<option value="teacher">教师</option>
<option value="student">学生</option>
</select>
</div>
<button type="submit" name="register" class="btn btn-primary">注册</button>
</form>
</div>
</body>
</html>
session_start();
// 连接数据库
$host = 'localhost';
$db = 'textbook_management';
$user = 'root';
$pass = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Could not connect to the database $db :" . $e->getMessage());
}
if (isset($_POST['register'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$role = $_POST['role'];
$stmt = $pdo->prepare("INSERT INTO users (username, password, role) VALUES (:username, :password, :role)");
$stmt->execute(['username' => $username, 'password' => $password, 'role' => $role]);
echo "注册成功!";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登录</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>登录</h1>
<form action="login.php" method="post">
<div class="form-group">
<label for="username">用户名</label>
<input type="text" class="form-control" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">密码</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<button type="submit" name="login" class="btn btn-primary">登录</button>
</form>
</div>
</body>
</html>
session_start();
// 连接数据库
$host = 'localhost';
$db = 'textbook_management';
$user = 'root';
$pass = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Could not connect to the database $db :" . $e->getMessage());
}
if (isset($_POST['login'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
$stmt->execute(['username' => $username, 'password' => $password]);
$user = $stmt->fetch();
if ($user) {
$_SESSION['user'] = $user;
header('Location: dashboard.php');
} else {
echo "Invalid username or password.";
}
}
?>
session_start();
if (!isset($_SESSION['user']) || $_SESSION['user']['role'] !== 'admin') {
header('Location: login.php');
exit;
}
$pdo = new PDO("mysql:host=localhost;dbname=textbook_management;charset=utf8", "root", "");
if (isset($_POST['submit'])) {
$title = $_POST['title'];
$author = $_POST['author'];
$publisher = $_POST['publisher'];
$isbn = $_POST['isbn'];
$quantity = $_POST['quantity'];
$textbook = new Textbook($pdo);
$textbook->addTextbook($title, $author, $publisher, $isbn, $quantity);
header('Location: manage-textbooks.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>添加教材</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>添加教材</h1>
<form action="add-textbook.php" method="post">
<div class="form-group">
<label for="title">教材名称</label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="form-group">
<label for="author">作者</label>
<input type="text" class="form-control" id="author" name="author" required>
</div>
<div class="form-group">
<label for="publisher">出版社</label>
<input type="text" class="form-control" id="publisher" name="publisher" required>
</div>
<div class="form-group">
<label for="isbn">ISBN编号</label>
<input type="text" class="form-control" id="isbn" name="isbn" required>
</div>
<div class="form-group">
<label for="quantity">库存数量</label>
<input type="number" class="form-control" id="quantity" name="quantity" min="0" required>
</div>
<button type="submit" name="submit" class="btn btn-primary">添加</button>
</form>
</div>
</body>
</html>
session_start();
if (!isset($_SESSION['user']) || $_SESSION['user']['role'] !== 'teacher') {
header('Location: login.php');
exit;
}
$pdo = new PDO("mysql:host=localhost;dbname=textbook_management;charset=utf8", "root", "");
$textbook = new Textbook($pdo);
$textbooks = $textbook->getAllTextbooks();
if (isset($_POST['submit'])) {
$user_id = $_SESSION['user']['id'];
$textbook_id = $_POST['textbook_id'];
$quantity = $_POST['quantity'];
$stmt = $pdo->prepare("INSERT INTO applications (user_id, textbook_id, quantity, status) VALUES (:user_id, :textbook_id, :quantity, 'pending')");
$stmt->execute(['user_id' => $user_id, 'textbook_id' => $textbook_id, 'quantity' => $quantity]);
header('Location: my-applications.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>申请教材</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>申请教材</h1>
<form action="apply-textbook.php" method="post">
<div class="form-group">
<label for="textbook_id">教材</label>
<select class="form-control" id="textbook_id" name="textbook_id" required>
<?php foreach ($textbooks as $textbook): ?>
<option value="$textbook['id']; ?>"><?php echo $textbook['title']; ?> (库存: <?php echo $textbook['quantity']; ?>)</option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="quantity">申请数量</label>
<input type="number" class="form-control" id="quantity" name="quantity" min="1" required>
</div>
<button type="submit" name="submit" class="btn btn-primary">申请</button>
</form>
</div>
</body>
</html>
为了保证系统的安全性,需要实现以下功能:
由于篇幅限制,无法完整展示所有源代码。以下是部分核心代码示例:
class Application {
private $pdo;
public function __construct($pdo) {
$this->pdo = $pdo;
}
public function getAllApplications() {
$stmt = $this->pdo->query("SELECT a.*, u.username, t.title FROM applications a JOIN users u ON a.user_id = u.id JOIN textbooks t ON a.textbook_id = t.id");
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function getApplicationsByUserId($user_id) {
$stmt = $this->pdo->prepare("SELECT a.*, t.title FROM applications a JOIN textbooks t ON a.textbook_id = t.id WHERE a.user_id = :user_id");
$stmt->execute(['user_id' => $user_id]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function approveApplication($id) {
$stmt = $this->pdo->prepare("UPDATE applications SET status = 'approved' WHERE id = :id");
$stmt->execute(['id' => $id]);
}
public function rejectApplication($id) {
$stmt = $this->pdo->prepare("UPDATE applications SET status = 'rejected' WHERE id = :id");
$stmt->execute(['id' => $id]);
}
}
?>
session_start();
if (!isset($_SESSION['user'])) {
header('Location: login.php');
exit;
}
$pdo = new PDO("mysql:host=localhost;dbname=textbook_management;charset=utf8", "root", "");
$application = new Application($pdo);
$applications = $application->getApplicationsByUserId($_SESSION['user']['id']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>我的申请</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>我的申请</h1>
<table class="table table-striped">
<thead>
<tr>
<th>教材名称</th>
<th>申请数量</th>
<th>申请状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($applications as $application): ?>
<tr>
<td><?php echo $application['title']; ?></td>
<td><?php echo $application['quantity']; ?></td>
<td><?php echo $application['status']; ?></td>
<td>
<?php if ($application['status'] === 'pending'): ?>
<a href="cancel-application.php?id=$application['id']; ?>" class="btn btn-danger">取消申请</a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<a href="apply-textbook.php" class="btn btn-primary">申请教材</a>
</div>
</body>
</html>