You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP+MySQL+JS删除功能失效:目标记录未删除问题求助

PHP+MySQL按ID删除记录功能故障解决

我正在完成PHP与MySQL的作业,需要实现点击按钮通过JavaScript根据ID删除表中记录的功能。完全跟着老师的视频操作,但就是删不掉目标记录。我和老师怀疑问题出在bind_param部分,但还没解决。相关文件如下:

相关文件代码

db-connect.php

<?php
    $servername = "localhost";
    $username = "root";
    $password = ""; // 此处应为空
    $dbName = "newDB"; // 填入你的数据库名
    
    $conn = new mysqli($servername, $username, $password, $dbName);
    
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    }
?>

index.php

<?php
    require "db-connect.php";
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>PHP Code-Along 2</title>

        <link rel="stylesheet" href="style.css">
    </head>

    <body>
        <h1>记录列表</h1>

        <div>
            <table>
                <tr>
                    <th>ID</th>
                    <th>姓名</th>
                    <th>年龄</th>
                    <th>邮箱</th>
                    <th>操作</th>
                </tr>
                <?php
                $sql = "SELECT * FROM `Employee`;";

                $sql_run = $conn->query($sql);

                if($sql_run) {  // 如果查询执行成功
                    if($sql_run->num_rows > 0) {    // 如果有返回结果
                        while($row = $sql_run->fetch_assoc()) {
                            ?>
                <tr>
                    <td><?= $row['id']; ?></td>
                    <td><?= $row['name']; ?></td>
                    <td><?= $row['age']; ?></td>
                    <td><?= $row['email']; ?></td>
                    <td>
                        <button onclick="document.location.href = 'form.php?id=<?= $row['id']; ?>'">编辑</button>
                        <button onclick="deleteConfirm(<?= $row['id']; ?>);">删除</button>
                    </td>
                </tr>
                <?php
                        }
                    } else {
                        ?>
                <tr>
                    <td colspan="5">无记录。</td>
                </tr>
                <?php
                    }
                } else {
                ?>
                <tr>
                    <td colspan="5">获取记录出错: <?= $conn->error; ?></td>
                </tr>
                <?php
                }
            ?>
            </table>
        </div>

        <script src="main.js"></script>
    </body>

</html>

main.js(原错误代码)

function deleteConfirm(id) {
    const response = confirm(`确定要删除ID为${id}的记录吗?`);
    if(response) {
        document.location.href = "db-deleterecord.php?=id" + id;
    }
}

db-deleterecord.php

<?php
 if(isset($_GET['id'])) {   // 检查是否存在?id=...参数
    require "db-connect.php";

    $sql = "DELETE FROM `Employee` WHERE `id` = ?;";

    $stmt = $conn->prepare($sql);

    if($stmt) {
        $stmt->bind_param("i", $_GET['id']);

        if($stmt->execute()) {
            echo "已删除ID为" .$_GET['id']. "的记录";
        } else echo "无法删除ID为" . $_GET['id'] . "的记录: " .$stmt->error;
    } else echo "无法预处理SQL语句: " . $conn->error;
 }
 header("refresh:5; url=index.php");
 ?>

问题定位与修复

核心错误:URL参数格式错误

原main.js中跳转URL的参数写法完全错误,把?id=写成了?=id,导致PHP端$_GET['id']根本获取不到有效的ID值,后续的删除逻辑自然无法执行。

修正后的main.js代码:

function deleteConfirm(id) {
    const response = confirm(`确定要删除ID为${id}的记录吗?`);
    if(response) {
        document.location.href = "db-deleterecord.php?id=" + id;
    }
}

额外优化建议

  1. 增加ID有效性校验:在db-deleterecord.php中可以添加对ID的合法性检查,避免非法参数:
    if(isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) {
        // 原有删除逻辑
    } else {
        echo "无效的记录ID";
    }
    
  2. 改用POST请求:删除操作属于修改数据的行为,建议使用POST请求而非GET,避免误触发或被缓存,提升安全性。

内容的提问来源于stack exchange,提问作者A.K. Enoch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 00:25:22