用php实现学生成绩管理系统完整代码
时间: 2023-12-31 18:04:47 浏览: 138
php学生成绩管理系统php
5星 · 资源好评率100%
抱歉,作为AI语言模型,我没有具体语言的开发能力。但是,以下是一个简单的学生成绩管理系统的实现思路和代码框架,供您参考:
1. 建立数据库
首先,需要在MySQL数据库中建立一个学生成绩管理系统的数据库,包含以下表:
- 学生表:包含学生ID、姓名、性别、年龄等字段;
- 课程表:包含课程ID、课程名称等字段;
- 成绩表:包含学生ID、课程ID、成绩等字段。
2. 连接数据库
使用PHP代码连接MySQL数据库,具体代码如下:
```
<?php
$host = "localhost"; // 数据库主机名
$username = "root"; // 数据库用户名
$password = "123456"; // 数据库密码
$dbname = "score_management"; // 数据库名
// 创建连接
$conn = mysqli_connect($host, $username, $password, $dbname);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
```
3. 实现学生信息管理功能
可以通过PHP代码实现学生信息的增删改查功能,具体代码如下:
```
<?php
// 添加学生信息
if ($_POST['action'] == 'add_student') {
$name = $_POST['name'];
$gender = $_POST['gender'];
$age = $_POST['age'];
$sql = "INSERT INTO student (name, gender, age) VALUES ('$name', '$gender', '$age')";
if (mysqli_query($conn, $sql)) {
echo "添加成功";
} else {
echo "添加失败";
}
}
// 删除学生信息
if ($_POST['action'] == 'delete_student') {
$id = $_POST['id'];
$sql = "DELETE FROM student WHERE id='$id'";
if (mysqli_query($conn, $sql)) {
echo "删除成功";
} else {
echo "删除失败";
}
}
// 修改学生信息
if ($_POST['action'] == 'update_student') {
$id = $_POST['id'];
$name = $_POST['name'];
$gender = $_POST['gender'];
$age = $_POST['age'];
$sql = "UPDATE student SET name='$name', gender='$gender', age='$age' WHERE id='$id'";
if (mysqli_query($conn, $sql)) {
echo "修改成功";
} else {
echo "修改失败";
}
}
// 查询学生信息
if ($_POST['action'] == 'select_student') {
$id = $_POST['id'];
$sql = "SELECT * FROM student WHERE id='$id'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
echo "姓名:" . $row['name'] . ",性别:" . $row['gender'] . ",年龄:" . $row['age'];
} else {
echo "未找到该学生信息";
}
}
?>
```
4. 实现课程信息管理功能
可以通过PHP代码实现课程信息的增删改查功能,具体代码如下:
```
<?php
// 添加课程信息
if ($_POST['action'] == 'add_course') {
$name = $_POST['name'];
$sql = "INSERT INTO course (name) VALUES ('$name')";
if (mysqli_query($conn, $sql)) {
echo "添加成功";
} else {
echo "添加失败";
}
}
// 删除课程信息
if ($_POST['action'] == 'delete_course') {
$id = $_POST['id'];
$sql = "DELETE FROM course WHERE id='$id'";
if (mysqli_query($conn, $sql)) {
echo "删除成功";
} else {
echo "删除失败";
}
}
// 修改课程信息
if ($_POST['action'] == 'update_course') {
$id = $_POST['id'];
$name = $_POST['name'];
$sql = "UPDATE course SET name='$name' WHERE id='$id'";
if (mysqli_query($conn, $sql)) {
echo "修改成功";
} else {
echo "修改失败";
}
}
// 查询课程信息
if ($_POST['action'] == 'select_course') {
$id = $_POST['id'];
$sql = "SELECT * FROM course WHERE id='$id'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
echo "课程名称:" . $row['name'];
} else {
echo "未找到该课程信息";
}
}
?>
```
5. 实现成绩信息管理功能
可以通过PHP代码实现成绩信息的增删改查功能,具体代码如下:
```
<?php
// 添加成绩信息
if ($_POST['action'] == 'add_score') {
$student_id = $_POST['student_id'];
$course_id = $_POST['course_id'];
$score = $_POST['score'];
$sql = "INSERT INTO score (student_id, course_id, score) VALUES ('$student_id', '$course_id', '$score')";
if (mysqli_query($conn, $sql)) {
echo "添加成功";
} else {
echo "添加失败";
}
}
// 删除成绩信息
if ($_POST['action'] == 'delete_score') {
$id = $_POST['id'];
$sql = "DELETE FROM score WHERE id='$id'";
if (mysqli_query($conn, $sql)) {
echo "删除成功";
} else {
echo "删除失败";
}
}
// 修改成绩信息
if ($_POST['action'] == 'update_score') {
$id = $_POST['id'];
$score = $_POST['score'];
$sql = "UPDATE score SET score='$score' WHERE id='$id'";
if (mysqli_query($conn, $sql)) {
echo "修改成功";
} else {
echo "修改失败";
}
}
// 查询成绩信息
if ($_POST['action'] == 'select_score') {
$id = $_POST['id'];
$sql = "SELECT * FROM score WHERE id='$id'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$student_id = $row['student_id'];
$course_id = $row['course_id'];
$score = $row['score'];
// 查询学生姓名和课程名称
$sql_student = "SELECT name FROM student WHERE id='$student_id'";
$result_student = mysqli_query($conn, $sql_student);
$row_student = mysqli_fetch_assoc($result_student);
$student_name = $row_student['name'];
$sql_course = "SELECT name FROM course WHERE id='$course_id'";
$result_course = mysqli_query($conn, $sql_course);
$row_course = mysqli_fetch_assoc($result_course);
$course_name = $row_course['name'];
echo "学生姓名:" . $student_name . ",课程名称:" . $course_name . ",成绩:" . $score;
} else {
echo "未找到该成绩信息";
}
}
?>
```
以上是一个简单的学生成绩管理系统的实现思路和代码框架,具体实现还需要根据具体需求进行调整和完善。
阅读全文