php+mysql做一个学生管理系统
时间: 2023-10-09 09:08:27 浏览: 251
学生管理系统是一个比较典型的信息管理系统,主要是用于管理学生的基本信息、课程成绩、考勤记录等信息。下面是一个基于PHP和MySQL的学生管理系统的简单实现:
1. 数据库设计
首先,我们需要设计数据库表结构。在MySQL中创建一个名为“student”的数据库,然后创建以下四个表:
- 学生表(students):包含学生的基本信息,如学号、姓名、性别、年龄等。
- 课程表(courses):包含学生选修的所有课程信息,如课程编号、课程名称、学分等。
- 成绩表(scores):包含学生的所有成绩信息,如学号、课程编号、成绩等。
- 考勤表(attendance):包含学生的所有考勤信息,如学号、课程编号、出勤情况等。
下面是数据库表结构的SQL语句:
```
-- 创建学生表
CREATE TABLE students (
id INT(11) NOT NULL AUTO_INCREMENT,
student_id VARCHAR(20) NOT NULL,
name VARCHAR(50) NOT NULL,
gender VARCHAR(10) NOT NULL,
age INT(11) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY (student_id)
);
-- 创建课程表
CREATE TABLE courses (
id INT(11) NOT NULL AUTO_INCREMENT,
course_id VARCHAR(20) NOT NULL,
name VARCHAR(50) NOT NULL,
credit INT(11) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY (course_id)
);
-- 创建成绩表
CREATE TABLE scores (
id INT(11) NOT NULL AUTO_INCREMENT,
student_id VARCHAR(20) NOT NULL,
course_id VARCHAR(20) NOT NULL,
score INT(11) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES students(student_id),
FOREIGN KEY (course_id) REFERENCES courses(course_id)
);
-- 创建考勤表
CREATE TABLE attendance (
id INT(11) NOT NULL AUTO_INCREMENT,
student_id VARCHAR(20) NOT NULL,
course_id VARCHAR(20) NOT NULL,
status VARCHAR(10) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES students(student_id),
FOREIGN KEY (course_id) REFERENCES courses(course_id)
);
```
2. 网页界面设计
学生管理系统的前端界面可以使用HTML、CSS和JavaScript等技术来实现。以下是一个简单的学生信息管理界面的设计:
```
<!DOCTYPE html>
<html>
<head>
<title>学生信息管理系统</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>学生信息管理系统</h1>
<div class="tab">
<button class="tablinks" onclick="openTab(event, 'students')">学生信息</button>
<button class="tablinks" onclick="openTab(event, 'courses')">课程信息</button>
<button class="tablinks" onclick="openTab(event, 'scores')">成绩管理</button>
<button class="tablinks" onclick="openTab(event, 'attendance')">考勤管理</button>
</div>
<div id="students" class="tabcontent">
<h2>学生信息管理</h2>
<form method="post" action="students.php">
<label for="student_id">学生编号:</label>
<input type="text" id="student_id" name="student_id">
<label for="name">姓名:</label>
<input type="text" id="name" name="name">
<label for="gender">性别:</label>
<select id="gender" name="gender">
<option value="男">男</option>
<option value="女">女</option>
</select>
<label for="age">年龄:</label>
<input type="text" id="age" name="age">
<input type="submit" value="添加">
</form>
<table>
<tr>
<th>学生编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
</tr>
<?php
// 查询学生信息并显示在表格中
$conn = mysqli_connect("localhost", "root", "123456", "student");
if (!$conn) {
die("连接失败:" . mysqli_connect_error());
}
$sql = "SELECT * FROM students";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>" . $row["student_id"] . "</td><td>" . $row["name"] . "</td><td>" . $row["gender"] . "</td><td>" . $row["age"] . "</td></tr>";
}
} else {
echo "<tr><td colspan='4'>暂无学生信息</td></tr>";
}
mysqli_close($conn);
?>
</table>
</div>
<div id="courses" class="tabcontent">
<h2>课程信息管理</h2>
<form method="post" action="courses.php">
<label for="course_id">课程编号:</label>
<input type="text" id="course_id" name="course_id">
<label for="name">课程名称:</label>
<input type="text" id="name" name="name">
<label for="credit">学分:</label>
<input type="text" id="credit" name="credit">
<input type="submit" value="添加">
</form>
<table>
<tr>
<th>课程编号</th>
<th>课程名称</th>
<th>学分</th>
</tr>
<?php
// 查询课程信息并显示在表格中
$conn = mysqli_connect("localhost", "root", "123456", "student");
if (!$conn) {
die("连接失败:" . mysqli_connect_error());
}
$sql = "SELECT * FROM courses";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>" . $row["course_id"] . "</td><td>" . $row["name"] . "</td><td>" . $row["credit"] . "</td></tr>";
}
} else {
echo "<tr><td colspan='3'>暂无课程信息</td></tr>";
}
mysqli_close($conn);
?>
</table>
</div>
<div id="scores" class="tabcontent">
<h2>成绩管理</h2>
<form method="post" action="scores.php">
<label for="student_id">学生编号:</label>
<input type="text" id="student_id" name="student_id">
<label for="course_id">课程编号:</label>
<input type="text" id="course_id" name="course_id">
<label for="score">成绩:</label>
<input type="text" id="score" name="score">
<input type="submit" value="添加">
</form>
<table>
<tr>
<th>学生编号</th>
<th>课程编号</th>
<th>成绩</th>
</tr>
<?php
// 查询成绩信息并显示在表格中
$conn = mysqli_connect("localhost", "root", "123456", "student");
if (!$conn) {
die("连接失败:" . mysqli_connect_error());
}
$sql = "SELECT * FROM scores";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>" . $row["student_id"] . "</td><td>" . $row["course_id"] . "</td><td>" . $row["score"] . "</td></tr>";
}
} else {
echo "<tr><td colspan='3'>暂无成绩信息</td></tr>";
}
mysqli_close($conn);
?>
</table>
</div>
<div id="attendance" class="tabcontent">
<h2>考勤管理</h2>
<form method="post" action="attendance.php">
<label for="student_id">学生编号:</label>
<input type="text" id="student_id" name="student_id">
<label for="course_id">课程编号:</label>
<input type="text" id="course_id" name="course_id">
<label for="status">出勤情况:</label>
<select id="status" name="status">
<option value="正常">正常</option>
<option value="迟到">迟到</option>
<option value="旷课">旷课</option>
</select>
<input type="submit" value="添加">
</form>
<table>
<tr>
<th>学生编号</th>
<th>课程编号</th>
<th>出勤情况</th>
</tr>
<?php
// 查询考勤信息并显示在表格中
$conn = mysqli_connect("localhost", "root", "123456", "student");
if (!$conn) {
die("连接失败:" . mysqli_connect_error());
}
$sql = "SELECT * FROM attendance";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>" . $row["student_id"] . "</td><td>" . $row["course_id"] . "</td><td>" . $row["status"] . "</td></tr>";
}
} else {
echo "<tr><td colspan='3'>暂无考勤信息</td></tr>";
}
mysqli_close($conn);
?>
</table>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
```
3. PHP后端代码实现
后端代码主要是用PHP语言来实现对数据库的增删改查操作。以下是一个简单的示例:
- 添加学生信息(students.php)
```
<?php
$student_id = $_POST["student_id"];
$name = $_POST["name"];
$gender = $_POST["gender"];
$age = $_POST["age"];
$conn = mysqli_connect("localhost", "root", "123456", "student");
if (!$conn) {
die("连接失败:" . mysqli_connect_error());
}
$sql = "INSERT INTO students (student_id, name, gender, age) VALUES ('$student_id', '$name', '$gender', '$age')";
if (mysqli_query($conn, $sql)) {
echo "<script>alert('添加成功');</script>";
} else {
echo "<script>alert('添加失败');</script>";
}
mysqli_close($conn);
echo "<script>window.location.href='index.php#students';</script>";
?>
```
- 添加课程信息(courses.php)
```
<?php
$course_id = $_POST["course_id"];
$name = $_POST["name"];
$credit = $_POST["credit"];
$conn = mysqli_connect("localhost", "root", "123456", "student");
if (!$conn) {
die("连接失败:" . mysqli_connect_error());
}
$sql = "INSERT INTO courses (course_id, name, credit) VALUES ('$course_id', '$name', '$credit')";
if (mysqli_query($conn, $sql)) {
echo "<script>alert('添加成功');</script>";
} else {
echo "<script>alert('添加失败');</script>";
}
mysqli_close($conn);
echo "<script>window.location.href='index.php#courses';</script>";
?>
```
- 添加成绩信息(scores.php)
```
<?php
$student_id = $_POST["student_id"];
$course_id = $_POST["course_id"];
$score = $_POST["score"];
$conn = mysqli_connect("localhost", "root", "123456", "student");
if (!$conn) {
die("连接失败:" . mysqli_connect_error());
}
$sql = "INSERT INTO scores (student_id, course_id, score) VALUES ('$student_id', '$course_id', '$score')";
if (mysqli_query($conn, $sql)) {
echo "<script>alert('添加成功');</script>";
} else {
echo "<script>alert('添加失败');</script>";
}
mysqli_close($conn);
echo "<script>window.location.href='index.php#scores';</script>";
?>
```
- 添加考勤信息(attendance.php)
```
<?php
$student_id = $_POST["student_id"];
$course_id = $_POST["course_id"];
$status = $_POST["status"];
$conn = mysqli_connect("localhost", "root", "123456", "student");
if (!$conn) {
die("连接失败:" . mysqli_connect_error());
}
$sql = "INSERT INTO attendance (student_id, course_id, status) VALUES ('$student_id', '$course_id', '$status')";
if (mysqli_query($conn, $sql)) {
echo "<script>alert('添加成功');</script>";
} else {
echo "<script>alert('添加失败');</script>";
}
mysqli_close($conn);
echo "<script>window.location.href='index.php#attendance';</script>";
?>
```
以上是一个基于PHP和MySQL的学生管理系统的简单实现,只是一个简单的示例,实际应用中还需要根据具体需求进行修改和完善。
阅读全文