ѧ����Ϣ�б� ����ѧ��... ����ѧ�� ID ���� ���� �Ա� רҵ ���� û�м�¼
时间: 2025-06-05 22:14:52 浏览: 17
根据你的问题,我理解你希望构建一个学生信息管理系统,并且需要展示学生的信息表。以下是一个完整的PHP代码示例,展示如何创建一个学生信息管理系统,并包含学生信息的表格展示。
### 学生信息管理系统
#### 1. 数据库配置
首先,我们需要创建一个数据库配置文件,用于存储数据库连接信息。
```php
<?php
// config.php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student_management";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
?>
```
#### 2. 创建学生信息表
接下来,我们需要在数据库中创建一个学生信息表 `students`。
```sql
CREATE TABLE students (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
age INT(3) NOT NULL,
gender ENUM('Male', 'Female') NOT NULL,
major VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```
#### 3. 显示学生信息
现在我们编写PHP代码来显示学生信息表。
```php
<?php
include 'config.php';
$sql = "SELECT * FROM students";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>学生信息管理系统</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 10px;
text-align: left;
}
</style>
</head>
<body>
<h1>学生信息列表</h1>
<table>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>专业</th>
<th>操作</th>
</tr>
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>" . $row["age"] . "</td>";
echo "<td>" . $row["gender"] . "</td>";
echo "<td>" . $row["major"] . "</td>";
echo "<td><a href='edit_student.php?id=" . $row["id"] . "'>编辑</a> | <a href='delete_student.php?id=" . $row["id"] . "' onclick=\"return confirm('确定要删除吗?')\">删除</a></td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='6'>没有记录</td></tr>";
}
?>
</table>
</body>
</html>
<?php
$conn->close();
?>
```
### 解释
1. **`config.php`**:此文件用于配置数据库连接信息,包括服务器地址、用户名、密码和数据库名称。
2. **SQL查询**:通过`SELECT * FROM students`查询所有学生信息,并将其显示在一个HTML表格中。
3. **HTML表格**:使用HTML `<table>` 标签来格式化学生信息的显示。
4. **操作列**:为每个学生提供“编辑”和“删除”链接,方便用户对学生的数据进行修改或删除。
###
阅读全文
相关推荐















