php+script实现简单的学生管理系统

使用PHP和PDO连接MySQL数据库,创建了一个简易学生管理系统。系统包含6个PHP文件,主要用于展示学生信息。
摘要由CSDN通过智能技术生成

学了php后写了一个简单的学生管理系统,因为就是一个简单的练习样式都没怎么写。

数据库用的是MySQL,用pdo链接的数据库。

数据库的格式为:

student
id int  
name varchar 50
sex varchar 2
age tinyint  
class varchar 50

php文件一共6个。分别是

1、index.php主要是罗列学生的信息

<div>
    <?php include_once('menu.php');?>
    <table border="1" align="center" width=90%>
        <caption><h1>浏览学生信息</h1></caption>
        <tr bgcolor="#cccccc">
            <th>ID</th><th>姓名</th><th>性别</th><th>年龄</th><th>班级</th><th>操作</th>
        </tr>
        <?php
        //链接数据库
        try{
            $pdo = new PDO("mysql:dbname=mydatabase;host=localhost","root","");
        }catch(PDOException $e){
            die("数据库链接失败".$e->getMessage());
        }
        //查询信息
        $sql = "select * from student";
        $result = $pdo->query($sql);
        foreach($result as $row){
 //循环罗列学生的信息
            echo '<tr>';
            echo "<td>{
 $row['id']}</td>";
            echo "<td>{
 $row['name']}</td>";
            echo "<td>{
 $row['sex']}</td>";
            echo "<td>{
 $row['age']}</td>";
            echo "<td>{
 $row['class']}</td>";
            echo "<td>
                   <a href='javascript:doDel({
 $row['id']})'>删除</a>
                   <a href='edit.php?id={
 $row['id']}')'>修改</a>
                </td>";
            echo '</tr>';
        }
        ?>
       </table>
</div>
<script>
    function doDel(id) {
  //当用户要删除信息的时候,弹出框,同时向删除也传递一个参数
        if (confirm("确定要删除吗?")) {
            window.location = 'action.php?action=del&id=' + id;
        }
<
学生管理系统是一个比较典型的信息管理系统,主要是用于管理学生的基本信息、课程成绩、考勤记录等信息。下面是一个基于PHPMySQL学生管理系统简单实现: 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>"; ?> ``` 以上是一个基于PHPMySQL学生管理系统简单实现,只是一个简单的示例,实际应用中还需要根据具体需求进行修改和完善。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值