
本文详解如何修正 php 中循环嵌套表格导致的多表问题,并规范 html 结构与 css 样式应用,实现用户数据在单一、语义化 `
$row->fname | 错误地将 ID 列内容赋给了 fname,现按实际字段名(id, fname, lname...)严格映射,并在首列显示 id(原代码缺失)。
<?php session_start();
mysqli_report(MYSQLI_REPORT_ALL ^ MYSQLI_REPORT_INDEX);
$initials = parse_ini_file("../.ht.asetukset.ini");
try {
$connection = mysqli_connect(
$initials["databaseserver"],
$initials["username"],
$initials["password"],
$initials["database"]
);
} catch (Exception $e) {
header("Location:../html/yhteysvirhe.html");
exit;
}
// 执行查询(建议使用预处理语句防止 SQL 注入,此处为简化示例)
$result = mysqli_query($connection, "SELECT id, fname, lname, uname, email, paswd, descrip FROM users");
?><!-- 引入 HTML 头部 --><?php include "../html/admin_header.html"; ?><!-- 主要内容区:确保仅有一个 <table> --><div class="sec">
<h1 class="padding"><b>Users list:</b></h1>
<br><table>
<!-- 表头:固定输出一次 --><thead><tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>User Name</th>
<th>Email</th>
<th>Password</th>
<th>Description</th>
<th>Delete</th>
<th>Edit</th>
</tr></thead>
<!-- 表体:循环输出每一行数据 --><tbody>
<?php while ($row = mysqli_fetch_object($result)): ?><tr>
<td><strong>= htmlspecialchars($row->id) ?></strong></td>
<td>= htmlspecialchars($row->fname) ?></td>
<td>= htmlspecialchars($row->lname) ?></td>
<td>= htmlspecialchars($row->uname) ?></td>
<td>= htmlspecialchars($row->email) ?></td>
<td><code>= htmlspecialchars($row->paswd) ?></code></td>
<td>= htmlspecialchars($row->descrip) ?></td>
<td>
<a href="./admin_remove.php?poistettava=<?=%20%24row->id%20?>" style="color: white; padding: 4px 12px; background-color: #dc3545; border-radius: 4px; text-decoration: none;">
Delete
</a>
</td>
<td>
<a href="./admin_edit.php?editable=<?=%20%24row->id%20?>" style="color: white; padding: 4px 12px; background-color: #007bff; border-radius: 4px; text-decoration: none;">
Edit
</a>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
<?php mysqli_close($connection);
include "../html/admin_footer.html";
?>
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











