删除数据后index.php数据表无法显示,点击删除按钮出现空白页
表单系统删除功能异常排查与修复方案
问题描述
我搭建了一个连接MySQL的表单系统,insert.php为表单页面,填写表单提交后数据可存入MySQL,且能在index.php中显示数据表。但点击删除按钮时页面会变成空白,完成删除操作后index.php也无法正常显示数据表。以下是相关代码:
index.php代码
<?php session_start(); if (isset($_POST["submit"])) { $ime = $_POST["ime"]; $prezime = $_POST["prezime"]; $pozicija = $_POST["pozicija"]; if (!empty($ime) && !empty($prezime) && !empty($pozicija)) { /*host, username, password, dbname*/ $link = mysqli_connect("localhost", "root", "", "forma"); // Check connection if ($link === false) { die("ERROR: Could not connect. " . mysqli_connect_error()); } // Attempt insert query execution $sql = "INSERT INTO forma (Ime, Prezime, Pozicija) VALUES ('$ime', '$prezime' , '$pozicija')"; if (mysqli_query($link, $sql)) { echo "Records inserted successfully." . "<br>"; } else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); } $podaci = "SELECT * FROM forma"; $rezultat = mysqli_query($link, $podaci); if (!$rezultat) { die(mysqli_error($link)); } if (mysqli_num_rows($rezultat) > 0) { foreach ($rezultat as $rez) { foreach ($rez as $key => $value) { echo ucfirst($key) . ": " . $value . "<br>"; } } } // Close connection mysqli_close($link); } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <?php if (isset($_SESSION['status'])) { ?> <div class="alert alert-warning alert-dismissible fade show" role="alert"> <strong>Hey!</strong> <?php echo $_SESSION['status']; ?> <button type="button" class="btn" data-bs-dismiss="alert" aria-label="Close"></button> </div> <?php unset($_SESSION['status']); } ?> <form action="delete.php" method="POST"> <table class="table"> <tbody> <tr> <th> <button type="submit" name="delete" class="btn-delete">Delete</button> </th> <th>ID</th> <th>Ime</th> <th>Prezime</th> <th>Pozicija</th> </tr> </tbody> <tbody> <?php $con = mysqli_connect("localhost", "root", "", "forma"); $query = "SELECT * FROM forma"; $query_run = mysqli_query($con, $query); if (mysqli_num_rows($query_run) > 0) { foreach ($query_run as $row) { ?> <tr> <td> <input type="checkbox" name="delete_id[]" value="<?= $row['ID']; ?>"> </td> <td><?= $row['ID']; ?></td> <td><?= $row['Ime']; ?></td> <td><?= $row['Prezime']; ?></td> <td><?= $row['Pozicija']; ?></td> </tr> <?php } } else { ?> <tr> <td colspan="5">No Record Found</td> </tr> <?php } ?> </tbody> </table> </form> </body> </html>
insert.php代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Form</title> </head> <body> <form action="index.php" method="post"> <label for="ime">Ime</label> <input type="text" name="ime" required><br> <label for="prezime">Prezime</label> <input type="text" name="prezime" required><br> <label for="pozicija">Pozicija</label> <input type="text" name="pozicija" required><br> <button type="submit" name="submit">Submit</button><br> </form> </body> </html>
delete.php代码
<?php //delete session_start(); $con = mysqli_connect("localhost", "root", "", "forma"); if (isset($_POST['delete']) && isset($_POST['delete_id']) && !empty($_POST['delete_id'])) { // 过滤ID为整数,防止SQL注入 $all_id = array_map('intval', $_POST['delete_id']); $extract_id = implode(',', $all_id); $query = "DELETE FROM forma WHERE ID IN($extract_id) "; $query_run = mysqli_query($con, $query); if ($query_run) { $_SESSION['status'] = "Multiple Data Deleted Successfully"; header("Location: index.php"); exit(); } else { $_SESSION['status'] = "Multiple Data Not Deleted: " . mysqli_error($con); header("Location: index.php"); exit(); } } else { $_SESSION['status'] = "Please select at least one record to delete"; header("Location: index.php"); exit(); } ?>
问题根源分析
- index.php逻辑范围错误:原代码中整个HTML页面和删除表单都被包裹在
if (isset($_POST["submit"]))判断内,只有提交表单时才会渲染页面。删除后跳转回index.php时无submit参数,导致页面无输出,表现为空白。 - 字段名拼写不一致:原SQL中使用
Poziicija(多一个字母i),与表单字段pozicija、显示表头Pozicija不统一,可能导致数据显示异常或插入错误。 - delete.php无空值处理:未判断是否选择了要删除的记录,若直接提交删除按钮,
$_POST['delete_id']不存在会引发报错,导致页面空白。 - session_start()位置错误:原index.php中
session_start()放在HTML标签之后,会触发"headers already sent"警告,导致session提示信息无法正常显示。 - SQL注入风险:原delete.php直接将用户输入的ID拼入SQL语句,存在安全隐患。
解决步骤总结
- 拆分index.php逻辑:将插入数据的代码保留在
if (isset($_POST["submit"]))内,HTML页面和数据展示部分移到判断外,确保页面始终能正常渲染。 - 统一字段名:将所有SQL语句中的
Poziicija改为Pozicija,保持与表单和表头一致。 - 修复delete.php的空值判断:添加对
$_POST['delete_id']的存在性和非空判断,避免无选择时报错。 - 调整session_start()位置:将
session_start()移到index.php的PHP代码最开头,确保在任何输出之前启动session。 - 防范SQL注入:使用
array_map('intval', ...)过滤ID为整数,避免恶意SQL注入。 - 添加错误信息:在delete.php中加入
mysqli_error($con),便于排查删除失败的具体原因。
内容的提问来源于stack exchange,提问作者Hollyy
相关产品推荐
相关产品推荐

