用PHP MySQL写学生信息管理系统
时间: 2024-03-22 16:39:36 浏览: 52
以下是一个简单的使用 PHP 和 MySQL 实现学生信息管理系统的示例代码:
1. 创建数据库和表格:
```sql
CREATE DATABASE student;
USE student;
CREATE TABLE student_info (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
gender ENUM('male', 'female') NOT NULL,
birthdate DATE NOT NULL,
id_card VARCHAR(18) NOT NULL,
class VARCHAR(20) NOT NULL,
PRIMARY KEY (id)
);
```
2. 编写 PHP 程序:
```php
<?php
// 连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "student";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接是否成功
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 处理表单提交
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// 处理录入功能
if (isset($_POST["add"])) {
$name = $_POST["name"];
$gender = $_POST["gender"];
$birthdate = $_POST["birthdate"];
$id_card = $_POST["id_card"];
$class = $_POST["class"];
$sql = "INSERT INTO student_info (name, gender, birthdate, id_card, class) VALUES ('$name', '$gender', '$birthdate', '$id_card', '$class')";
if (mysqli_query($conn, $sql)) {
echo "Record added successfully";
} else {
echo "Error adding record: " . mysqli_error($conn);
}
}
// 处理查询功能
if (isset($_POST["search"])) {
$keyword = $_POST["keyword"];
$sql = "SELECT * FROM student_info WHERE name LIKE '%$keyword%' OR id_card LIKE '%$keyword%' OR class LIKE '%$keyword%'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<table>
<tr>
<th>Name</th>
<th>Gender</th>
<th>Birthdate</th>
<th>ID Card</th>
<th>Class</th>
</tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>" . $row["name"] . "</td>
<td>" . $row["gender"] . "</td>
<td>" . $row["birthdate"] . "</td>
<td>" . $row["id_card"] . "</td>
<td>" . $row["class"] . "</td>
</tr>";
}
echo "</table>";
} else {
echo "No results found";
}
}
// 处理修改功能
if (isset($_POST["update"])) {
$id = $_POST["id"];
$name = $_POST["name"];
$gender = $_POST["gender"];
$birthdate = $_POST["birthdate"];
$id_card = $_POST["id_card"];
$class = $_POST["class"];
$sql = "UPDATE student_info SET name='$name', gender='$gender', birthdate='$birthdate', id_card='$id_card', class='$class' WHERE id=$id";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
}
// 处理删除功能
if (isset($_POST["delete"])) {
$id = $_POST["id"];
$sql = "DELETE FROM student_info WHERE id=$id";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
}
}
// 关闭连接
mysqli_close($conn);
?>
```
3. 显示界面:
```html
<!DOCTYPE html>
<html>
<head>
<title>Student Information Management System</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
form {
border: 2px solid black;
padding: 20px;
width: 500px;
margin-bottom: 20px;
}
</style>
</head>
<body>
<h1>Student Information Management System</h1>
<!-- 录入表单 -->
<form method="post">
<h2>Add Record</h2>
<label>Name:</label>
<input type="text" name="name" required><br><br>
<label>Gender:</label>
<input type="radio" name="gender" value="male" required>Male
<input type="radio" name="gender" value="female" required>Female<br><br>
<label>Birthdate:</label>
<input type="date" name="birthdate" required><br><br>
<label>ID Card:</label>
<input type="text" name="id_card" required><br><br>
<label>Class:</label>
<input type="text" name="class" required><br><br>
<input type="submit" name="add" value="Add Record">
</form>
<!-- 查询表单 -->
<form method="post">
<h2>Search Record</h2>
<label>Keyword:</label>
<input type="text" name="keyword" required><br><br>
<input type="submit" name="search" value="Search">
</form>
<!-- 修改表单 -->
<form method="post">
<h2>Update Record</h2>
<label>ID:</label>
<input type="text" name="id" required><br><br>
<label>Name:</label>
<input type="text" name="name" required><br><br>
<label>Gender:</label>
<input type="radio" name="gender" value="male" required>Male
<input type="radio" name="gender" value="female" required>Female<br><br>
<label>Birthdate:</label>
<input type="date" name="birthdate" required><br><br>
<label>ID Card:</label>
<input type="text" name="id_card" required><br><br>
<label>Class:</label>
<input type="text" name="class" required><br><br>
<input type="submit" name="update" value="Update Record">
</form>
<!-- 删除表单 -->
<form method="post">
<h2>Delete Record</h2>
<label>ID:</label>
<input type="text" name="id" required><br><br>
<input type="submit" name="delete" value="Delete Record">
</form>
</body>
</html>
```
这个示例代码只是一个简单的学生信息管理系统,你可以根据自己的实际需要进行修改和完善。
阅读全文