学生信息管理系统php代码
时间: 2023-07-24 14:36:07 浏览: 60
好的,这里提供一个简单的学生信息管理系统的 PHP 代码示例:
index.php
```php
<?php
include('database.php');
$students = get_students();
$classes = get_classes();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息管理系统</title>
</head>
<body>
<h1>学生信息管理系统</h1>
<h2>学生列表</h2>
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>班级</th>
</tr>
</thead>
<tbody>
<?php foreach($students as $student): ?>
<tr>
<td><?php echo $student['name']; ?></td>
<td><?php echo $student['age']; ?></td>
<td><?php echo $student['gender']; ?></td>
<td><?php echo $student['class_name']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<h2>班级列表</h2>
<ul>
<?php foreach($classes as $class): ?>
<li><?php echo $class['name']; ?></li>
<?php endforeach; ?>
</ul>
</body>
</html>
```
database.php
```php
<?php
// 数据库连接信息
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "mydb";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接是否成功
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 获取所有学生信息
function get_students() {
global $conn;
$sql = "SELECT students.name, students.age, students.gender, classes.name as class_name FROM students LEFT JOIN classes ON students.class_id = classes.id";
$result = $conn->query($sql);
$students = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$students[] = $row;
}
}
return $students;
}
// 获取所有班级信息
function get_classes() {
global $conn;
$sql = "SELECT * FROM classes";
$result = $conn->query($sql);
$classes = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$classes[] = $row;
}
}
return $classes;
}
?>
```
这是一个简单的示例,其中包括了如何连接数据库、获取学生和班级信息,并在网页中展示。请注意,这只是一个示例,实际的学生信息管理系统需要根据具体需求进行设计和开发。