用web语言写出学生信息管理系统的代码
时间: 2023-12-10 16:05:33 浏览: 30
好的,下面是一个使用PHP语言和MySQL数据库的学生信息管理系统的基本代码。
1. 数据库连接
在使用MySQL数据库之前,需要先连接数据库。可以在PHP中使用mysqli_connect()函数来连接MySQL数据库,具体代码如下:
```php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接是否成功
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
echo "连接成功";
?>
```
2. 学生信息录入
可以使用HTML表单和PHP脚本来实现学生信息的录入,具体代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<title>学生信息录入</title>
</head>
<body>
<h1>学生信息录入</h1>
<form action="insert.php" method="post">
<label>姓名:</label>
<input type="text" name="name"><br>
<label>学号:</label>
<input type="text" name="student_id"><br>
<label>性别:</label>
<input type="radio" name="gender" value="male">男
<input type="radio" name="gender" value="female">女<br>
<label>年龄:</label>
<input type="text" name="age"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
```
在submit按钮被点击后,将调用insert.php脚本来将学生信息插入到数据库中。insert.php脚本的代码如下:
```php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接是否成功
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
// 获取表单数据
$name = $_POST['name'];
$student_id = $_POST['student_id'];
$gender = $_POST['gender'];
$age = $_POST['age'];
// 插入数据
$sql = "INSERT INTO students (name, student_id, gender, age) VALUES ('$name', '$student_id', '$gender', '$age')";
if (mysqli_query($conn, $sql)) {
echo "学生信息录入成功";
} else {
echo "学生信息录入失败: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
```
3. 学生信息查询
可以使用HTML表单和PHP脚本来实现学生信息的查询,具体代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<title>学生信息查询</title>
</head>
<body>
<h1>学生信息查询</h1>
<form action="search.php" method="post">
<label>姓名:</label>
<input type="text" name="name"><br>
<input type="submit" value="查询">
</form>
</body>
</html>
```
在submit按钮被点击后,将调用search.php脚本来查询学生信息。search.php脚本的代码如下:
```php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接是否成功
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
// 获取表单数据
$name = $_POST['name'];
// 查询数据
$sql = "SELECT * FROM students WHERE name = '$name'";
$result = mysqli_query($conn, $sql);
// 显示查询结果
if (mysqli_num_rows($result) > 0) {
// 输出每行数据
while($row = mysqli_fetch_assoc($result)) {
echo "姓名: " . $row["name"]. " 学号: " . $row["student_id"]. " 性别: " . $row["gender"]. " 年龄: " . $row["age"]. "<br>";
}
} else {
echo "没有查询到相关学生信息";
}
mysqli_close($conn);
?>
```
4. 学生信息修改
可以使用HTML表单和PHP脚本来实现学生信息的修改,具体代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<title>学生信息修改</title>
</head>
<body>
<h1>学生信息修改</h1>
<form action="update.php" method="post">
<label>学号:</label>
<input type="text" name="student_id"><br>
<label>年龄:</label>
<input type="text" name="age"><br>
<input type="submit" value="修改">
</form>
</body>
</html>
```
在submit按钮被点击后,将调用update.php脚本来修改学生信息。update.php脚本的代码如下:
```php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接是否成功
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
// 获取表单数据
$student_id = $_POST['student_id'];
$age = $_POST['age'];
// 更新数据
$sql = "UPDATE students SET age='$age' WHERE student_id='$student_id'";
if (mysqli_query($conn, $sql)) {
echo "学生信息修改成功";
} else {
echo "学生信息修改失败: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
```
5. 学生信息删除
可以使用HTML表单和PHP脚本来实现学生信息的删除,具体代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<title>学生信息删除</title>
</head>
<body>
<h1>学生信息删除</h1>
<form action="delete.php" method="post">
<label>学号:</label>
<input type="text" name="student_id"><br>
<input type="submit" value="删除">
</form>
</body>
</html>
```
在submit按钮被点击后,将调用delete.php脚本来删除学生信息。delete.php脚本的代码如下:
```php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接是否成功
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
// 获取表单数据
$student_id = $_POST['student_id'];
// 删除数据
$sql = "DELETE FROM students WHERE student_id='$student_id'";
if (mysqli_query($conn, $sql)) {
echo "学生信息删除成功";
} else {
echo "学生信息删除失败: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
```
以上代码只是学生信息管理系统的基本代码,实际使用中还需要进行更多的开发和优化才能实现更完整的功能。