代码生成一个php学生管理系统
时间: 2023-10-26 16:07:17 浏览: 77
好的,我可以给你提供一个简单的 PHP 学生管理系统的代码示例,但需要说明的是,这只是一个基础的示例,实际开发中需要根据具体需求进行设计和开发。
首先,你需要一个 MySQL 数据库,用于存储学生信息。在数据库中创建一个名为 `students` 的表,包含以下字段:
- `id`:学生 ID,自增长。
- `name`:学生姓名。
- `gender`:学生性别。
- `age`:学生年龄。
- `class`:学生所在班级。
接下来,你可以编写以下 PHP 代码实现学生信息的添加、查询和删除:
```php
<?php
// 连接数据库
$conn = mysqli_connect('localhost', 'root', '', 'your_database_name');
// 添加学生信息
if (isset($_POST['add'])) {
$name = $_POST['name'];
$gender = $_POST['gender'];
$age = $_POST['age'];
$class = $_POST['class'];
$sql = "INSERT INTO students (name, gender, age, class) VALUES ('$name', '$gender', '$age', '$class')";
mysqli_query($conn, $sql);
header('location: index.php');
}
// 查询学生信息
if (isset($_GET['id'])) {
$id = $_GET['id'];
$sql = "SELECT * FROM students WHERE id = $id";
$result = mysqli_query($conn, $sql);
$student = mysqli_fetch_assoc($result);
}
// 删除学生信息
if (isset($_GET['del'])) {
$id = $_GET['del'];
$sql = "DELETE FROM students WHERE id = $id";
mysqli_query($conn, $sql);
header('location: index.php');
}
// 查询所有学生信息
$sql = "SELECT * FROM students";
$result = mysqli_query($conn, $sql);
$students = mysqli_fetch_all($result, MYSQLI_ASSOC);
?>
<!-- 学生信息列表 -->
<table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>班级</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($students as $student) { ?>
<tr>
<td><?php echo $student['id']; ?></td>
<td><?php echo $student['name']; ?></td>
<td><?php echo $student['gender']; ?></td>
<td><?php echo $student['age']; ?></td>
<td><?php echo $student['class']; ?></td>
<td>
<a href="index.php?id=<?php echo $student['id']; ?>">编辑</a>
<a href="index.php?del=<?php echo $student['id']; ?>">删除</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<!-- 添加学生信息表单 -->
<form action="index.php" method="post">
<label>姓名:</label>
<input type="text" name="name"><br>
<label>性别:</label>
<input type="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女<br>
<label>年龄:</label>
<input type="number" name="age"><br>
<label>班级:</label>
<input type="text" name="class"><br>
<input type="submit" name="add" value="添加">
</form>
<!-- 编辑学生信息表单 -->
<?php if (isset($student)) { ?>
<form action="index.php?id=<?php echo $student['id']; ?>" method="post">
<label>姓名:</label>
<input type="text" name="name" value="<?php echo $student['name']; ?>"><br>
<label>性别:</label>
<input type="radio" name="gender" value="男" <?php if ($student['gender'] == '男') { echo 'checked'; } ?>>男
<input type="radio" name="gender" value="女" <?php if ($student['gender'] == '女') { echo 'checked'; } ?>>女<br>
<label>年龄:</label>
<input type="number" name="age" value="<?php echo $student['age']; ?>"><br>
<label>班级:</label>
<input type="text" name="class" value="<?php echo $student['class']; ?>"><br>
<input type="submit" name="update" value="更新">
</form>
<?php } ?>
```
以上代码实现了学生信息的添加、查询和删除,同时展示了学生信息的列表和表单。你可以根据自己的需求进行修改和扩展。
需要特别注意的是,上述代码没有进行任何的数据验证和安全处理,实际开发中需要考虑到各种可能的攻击和错误情况,进行相应的处理和防范。