要求
1.使用 navicat 建立一个学生信息数据库
至少包含一张表 student,该表包含 至少包括4 个字段(学 号 sno,姓名 sname,性别 ssex,专业 spro),创建好之后可以添加几条数据。 可以增加其它表。
2.使用 PHP 编程
(1) 制作静态页面,首先登录界面 login.php,输入数据库端口、账户、密码、选择的数据库、以及要使用的表,如果输入正确,则跳转到另一个显示该表中的所有数据的页面 list.php,如果错误则弹出相应的提示。
(2) 在 list.php 的页面中,每一条记录后面都有“修改”和“删除”按钮。点击修改之后,跳转到新的编辑页面 edit.php,该页面包含一个表单,每个表单控件的默认值都设置为该学生目前的信息值,当点击“提交”按钮后,可以将该学生的信息进行更改,并保存到数据库中,成功保存后跳回到 list.php 页面。当点击“删除”按钮时,如果成功删除数据库,则提示“数据库删除成功”,并返回 list.php 页面,删除失败时也要有失败的提示。
(3) 除此之外,list.php 中还应该包括一个“添加”的按钮,点击后跳转到新的add.php 中,包含一个空的表单,填写数据后点击“提交”,将新的记录添加到数据库中,并跳转回 list.php 页面中。
效果图:
1.navicat 软件
2.php
Login.php
List.php
Add.php
Delete.php
Edit.php
代码如下
login.php
<?php
// 启用错误报告(仅用于开发调试,生产环境中请移除)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
*/
// 处理登录表单提交
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$db_port = isset($_POST['db_port']) ? (int)$_POST['db_port'] : 3306;
$db_user = isset($_POST['db_user']) ? trim($_POST['db_user']) : '';
$db_pass = isset($_POST['db_pass']) ? trim($_POST['db_pass']) : '';
$db_name = isset($_POST['db_name']) ? trim($_POST['db_name']) : '';
$db_table = isset($_POST['db_table']) ? trim($_POST['db_table']) : '';
// 简单验证
if (empty($db_user) || empty($db_pass) || empty($db_name) || empty($db_table)) {
$error = "所有字段都是必填的";
} else {
// 尝试连接数据库
$conn = new mysqli('localhost', $db_user, $db_pass, $db_name, $db_port);
if ($conn->connect_error) {
$error = "数据库连接失败: " . $conn->connect_error;
} else {
// 检查表是否存在
$stmt = $conn->prepare("SELECT 1 FROM $db_table LIMIT 1");
if ($stmt) {
// 表存在
$stmt->close();
$conn->close();
// 跳转到 list.php 并传递参数
header("Location: list.php?db_port=$db_port&db_user=" . urlencode($db_user) . "&db_pass=" . urlencode($db_pass) . "&db_name=" . urlencode($db_name) . "&db_table=" . urlencode($db_table));
exit();
} else {
// 表不存在
$error = "指定的表不存在或无法访问。";
$conn->close();
}
}
}
}
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h2 align="center">登录页面</h2>
<form action="" method="post">
<table align="center">
<tr>
<td>数据库端口:</td>
<td><input type="number" name="db_port" value="3306" min="1" required></td>
</tr>
<tr>
<td>账户:</td>
<td><input type="text" name="db_user" required></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="db_pass" required></td>
</tr>
<tr>
<td>数据库名:</td>
<td><input type="text" name="db_name" required></td>
</tr>
<tr>
<td>表名:</td>
<td><input type="text" name="db_table" required></td>
</tr>
<?php if (isset($error)) { ?>
<tr>
<td colspan="2" style="color: red; text-align: center;"><?php echo htmlspecialchars($error); ?></td>
</tr>
<?php } ?>
<tr>
<td colspan="2" align="center"><input type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>
list.php
<?php
// 启用错误报告(仅用于开发调试,生产环境中请移除)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// 获取表单数据
$db_port = isset($_GET['db_port']) ? (int)$_GET['db_port'] : 3306; // 默认端口3306
$db_user = isset($_GET['db_user']) ? $_GET['db_user'] : 'root';
$db_pass = isset($_GET['db_pass']) ? $_GET['db_pass'] : 'root';
$db_name = isset($_GET['db_name']) ? $_GET['db_name'] : 'school';
$db_table = isset($_GET['db_table']) ? $_GET['db_table'] : 'student';
// 连接数据库
$conn = new mysqli('localhost', $db_user, $db_pass, $db_name, $db_port);
// 设置字符集为 UTF-8
$conn->set_charset("utf8");
// 检查连接
if ($conn->connect_error) {
die("登录失败: " . $conn->connect_error);
}
// 查询数据,明确指定字段顺序
$sql = "SELECT sno, sname, ssex, sage, spro FROM $db_table";
$result = $conn->query($sql);
if ($result === FALSE) {
die("查询失败: " . $conn->error);
}
if ($result->num_rows > 0) {
echo "<h1 align='center'>表数据</h1>";
echo "<table border='1' align='center'><tr>";
// 输出表头
$fields = ['sno' => '学号', 'sname' => '姓名', 'ssex' => '性别', 'sage' => '年龄', 'spro' => '专业'];
foreach ($fields as $field => $display_name) {
echo "<th>" . htmlspecialchars($display_name) . "</th>";
}
echo "<th>操作</th></tr>";
// 输出数据
while ($row = $result->fetch_assoc()) {
echo "<tr>";
foreach ($fields as $field => $display_name) {
echo "<td>" . htmlspecialchars($row[$field]) . "</td>";
}
echo "<td>
<a href='edit.php?sno=" . urlencode($row['sno']) . "&db_port=$db_port&db_user=" . urlencode($db_user) . "&db_pass=" . urlencode($db_pass) . "&db_name=$db_name&db_table=$db_table'>修改</a>
<a href='delete.php?sno=" . urlencode($row['sno']) . "&db_port=$db_port&db_user=" . urlencode($db_user) . "&db_pass=" . urlencode($db_pass) . "&db_name=$db_name&db_table=$db_table' onclick='return confirm(\"确定要删除这条记录吗?\")'>删除</a>
</td>";
echo "</tr>";
}
echo "</table>";
echo "<br><div style='text-align: center;'>";
echo "<button type='button' onclick=\"window.location.href='add.php?db_port=$db_port&db_user=" . urlencode($db_user) . "&db_pass=" . urlencode($db_pass) . "&db_name=$db_name&db_table=$db_table'\">添加新记录</button>";
echo "</div>";
} else {
echo "<p align='center'>没有数据</p>";
}
$conn->close();
?>
edit.php
<?php
// 启用错误报告(仅用于开发调试,生产环境中请移除)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// 获取 URL 参数
$sno = isset($_GET['sno']) ? $_GET['sno'] : '';
$db_port = isset($_GET['db_port']) ? (int)$_GET['db_port'] : 3306; // 默认端口3306
$db_user = isset($_GET['db_user']) ? $_GET['db_user'] : 'root';
$db_pass = isset($_GET['db_pass']) ? $_GET['db_pass'] : 'root';
$db_name = isset($_GET['db_name']) ? $_GET['db_name'] : 'school';
$db_table = isset($_GET['db_table']) ? $_GET['db_table'] : 'student';
// 检查 sno 是否为空
if (empty($sno)) {
die("错误: 学号 (sno) 不能为空");
}
// 连接数据库
$conn = new mysqli('localhost', $db_user, $db_pass, $db_name, $db_port);
// 设置字符集为 UTF-8
$conn->set_charset("utf8");
// 检查连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 获取当前记录
$stmt = $conn->prepare("SELECT sno, sname, ssex, sage, spro FROM $db_table WHERE sno = ?");
$stmt->bind_param("s", $sno);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
die("没有找到学号为 " . htmlspecialchars($sno) . " 的记录");
}
$row = $result->fetch_assoc();
// 处理表单提交
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// 获取并验证表单数据
$new_sno = isset($_POST['sno']) ? trim($_POST['sno']) : '';
$sname = isset($_POST['sname']) ? trim($_POST['sname']) : '';
$ssex = isset($_POST['ssex']) ? trim($_POST['ssex']) : '';
$sage = isset($_POST['sage']) ? (int)$_POST['sage'] : 0;
$spro = isset($_POST['spro']) ? trim($_POST['spro']) : '';
// 简单验证(可根据需要添加更多验证)
if (empty($new_sno) || empty($sname) || empty($ssex) || empty($spro)) {
echo "<script>alert('所有字段都是必填的');</script>";
} elseif ($sage <= 0) {
echo "<script>alert('年龄必须是一个正整数');</script>";
} else {
// 使用预处理语句更新记录
$update_stmt = $conn->prepare("UPDATE $db_table SET sno = ?, sname = ?, ssex = ?, sage = ?, spro = ? WHERE sno = ?");
$update_stmt->bind_param("sssiss", $new_sno, $sname, $ssex, $sage, $spro, $sno);
if ($update_stmt->execute()) {
echo "<script>
alert('记录更新成功');
window.location.href='list.php?db_port=$db_port&db_user=" . urlencode($db_user) . "&db_pass=" . urlencode($db_pass) . "&db_name=$db_name&db_table=$db_table';
</script>";
exit();
} else {
echo "<script>alert('更新记录时出错: " . addslashes($update_stmt->error) . "');</script>";
}
$update_stmt->close();
}
}
$stmt->close();
$conn->close();
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>编辑记录</title>
</head>
<body>
<h1 align="center">编辑记录</h1>
<form action="" method="post">
<table align="center">
<tr>
<td>学号:</td>
<td><input type="text" name="sno" value="<?php echo htmlspecialchars($row['sno']); ?>" required></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="sname" value="<?php echo htmlspecialchars($row['sname']); ?>" required></td>
</tr>
<tr>
<td>性别:</td>
<td>
<select name="ssex" required>
<option value="男" <?php if ($row['ssex'] == '男') echo 'selected'; ?>>男</option>
<option value="女" <?php if ($row['ssex'] == '女') echo 'selected'; ?>>女</option>
</select>
</td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="number" name="sage" value="<?php echo htmlspecialchars($row['sage']); ?>" min="1" required></td>
</tr>
<tr>
<td>专业:</td>
<td><input type="text" name="spro" value="<?php echo htmlspecialchars($row['spro']); ?>" required></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="提交">
<a href="list.php?db_port=<?php echo $db_port; ?>&db_user=<?php echo urlencode($db_user); ?>&db_pass=<?php echo urlencode($db_pass); ?>&db_name=<?php echo $db_name; ?>&db_table=<?php echo $db_table; ?>">
<button type="button">返回</button>
</a>
</td>
</tr>
</table>
</form>
</body>
</html>
delete.php
<?php
// 启用错误报告(仅用于开发调试,生产环境中请移除)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// 获取 URL 参数
$sno = isset($_GET['sno']) ? $_GET['sno'] : '';
$db_port = isset($_GET['db_port']) ? (int)$_GET['db_port'] : 3306; // 默认端口3306
$db_user = isset($_GET['db_user']) ? $_GET['db_user'] : 'root';
$db_pass = isset($_GET['db_pass']) ? $_GET['db_pass'] : 'root';
$db_name = isset($_GET['db_name']) ? $_GET['db_name'] : 'school';
$db_table = isset($_GET['db_table']) ? $_GET['db_table'] : 'student';
// 检查 sno 是否为空
if (empty($sno)) {
die("错误: 学号 (sno) 不能为空");
}
// 连接数据库
$conn = new mysqli('localhost', $db_user, $db_pass, $db_name, $db_port);
// 设置字符集为 UTF-8
$conn->set_charset("utf8");
// 检查连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 使用预处理语句防止SQL注入
$stmt = $conn->prepare("DELETE FROM $db_table WHERE sno = ?");
$stmt->bind_param("s", $sno);
if ($stmt->execute()) {
echo "<script>
alert('数据库删除成功');
window.location.href='list.php?db_port=$db_port&db_user=" . urlencode($db_user) . "&db_pass=" . urlencode($db_pass) . "&db_name=$db_name&db_table=$db_table';
</script>";
} else {
echo "<script>
alert('删除记录时出错: " . addslashes($stmt->error) . "');
window.location.href='list.php?db_port=$db_port&db_user=" . urlencode($db_user) . "&db_pass=" . urlencode($db_pass) . "&db_name=$db_name&db_table=$db_table';
</script>";
}
$stmt->close();
$conn->close();
?>
add.php
<?php
// 获取 URL 参数
$db_port = isset($_GET['db_port']) ? (int)$_GET['db_port'] : 3306; // 默认端口3306
$db_user = isset($_GET['db_user']) ? $_GET['db_user'] : 'root';
$db_pass = isset($_GET['db_pass']) ? $_GET['db_pass'] : 'root';
$db_name = isset($_GET['db_name']) ? $_GET['db_name'] : 'school';
$db_table = isset($_GET['db_table']) ? $_GET['db_table'] : 'student';
// 连接数据库
$conn = new mysqli('localhost', $db_user, $db_pass, $db_name, $db_port);
// 设置字符集为 UTF-8
$conn->set_charset("utf8");
// 检查连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 处理表单提交
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// 获取并验证表单数据
$sno = isset($_POST['sno']) ? trim($_POST['sno']) : '';
$sname = isset($_POST['sname']) ? trim($_POST['sname']) : '';
$ssex = isset($_POST['ssex']) ? trim($_POST['ssex']) : '';
$sage = isset($_POST['sage']) ? (int)$_POST['sage'] : 0;
$spro = isset($_POST['spro']) ? trim($_POST['spro']) : '';
// 简单验证(可根据需要添加更多验证)
if (empty($sno) || empty($sname) || empty($ssex) || empty($spro)) {
echo "<script>alert('所有字段都是必填的');</script>";
} elseif ($sage <= 0) {
echo "<script>alert('年龄必须是一个正整数');</script>";
} else {
// 使用预处理语句插入记录
$stmt = $conn->prepare("INSERT INTO $db_table (sno, sname, ssex, sage, spro) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sssis", $sno, $sname, $ssex, $sage, $spro);
if ($stmt->execute()) {
echo "<script>
alert('记录添加成功');
window.location.href='list.php?db_port=$db_port&db_user=" . urlencode($db_user) . "&db_pass=" . urlencode($db_pass) . "&db_name=$db_name&db_table=$db_table';
</script>";
exit();
} else {
echo "<script>alert('添加记录时出错: " . addslashes($stmt->error) . "');</script>";
}
$stmt->close();
}
}
$conn->close();
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>添加新记录</title>
</head>
<body>
<h1 align="center">添加新记录</h1>
<form action="" method="post">
<table align="center">
<tr>
<td>学号:</td>
<td><input type="text" name="sno" required></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="sname" required></td>
</tr>
<tr>
<td>性别:</td>
<td>
<select name="ssex" required>
<option value="">选择性别</option>
<option value="男">男</option>
<option value="女">女</option>
</select>
</td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="number" name="sage" min="1" required></td>
</tr>
<tr>
<td>专业:</td>
<td><input type="text" name="spro" required></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="提交">
<a href="list.php?db_port=<?php echo $db_port; ?>&db_user=<?php echo urlencode($db_user); ?>&db_pass=<?php echo urlencode($db_pass); ?>&db_name=<?php echo $db_name; ?>&db_table=<?php echo $db_table; ?>">
<button type="button">返回</button>
</a>
</td>
</tr>
</table>
</form>
</body>
</html>