php 为什么我想删除的记录仍然存在,即使我已经启动并运行了该功能?

bfnvny8b  于 2022-11-28  发布在  PHP
关注(0)|答案(1)|浏览(225)

我正在做一个PHP和MySQL的作业,我需要创建一个函数,通过ID从表中删除记录,只需按下JavaScript按钮。即使我跟着老师的视频,它仍然无法删除记录。
我的老师和我怀疑这与bind_param部分有关,但仍然没有解决。
文件如下:
db-connect.php

<?php
    $servername = "localhost";
    $username = "root";
    $password = ""; // this should be empty
    $dbName = "newDB"; // add your database name here
    
    $conn = new mysqli($servername, $username, $password, $dbName);
    
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    
        /**
         * $conn->connect_error - contains an error message from the database server (if any)
         */
    }
    ?>

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>List of Records</h1>

        <div>
            <table>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Age</th>
                    <th>Email</th>
                    <th>Actions</th>
                </tr>
                <?php
                $sql = "SELECT * FROM `Employee`;";

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

                if($sql_run) {  // if it is not false, then proceed
                    if($sql_run->num_rows > 0) {    // num_rows will check if there are row(s) of results
                        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']; ?>'">Edit</button>
                        <button onclick="deleteConfirm(<?= $row['id']; ?>);">Delete</button>
                    </td>
                </tr>
                <?php
                        }
                    } else {
                        // echo "No table rows found.";
                        ?>
                <tr>
                    <td colspan="5">No records found.</td>
                </tr>
                <?php
                    }
                } else {
                ?>
                <tr>
                    <td colspan="5">Error retrieving table rows: <?= $conn->error; ?></td>
                </tr>
                <?php
                }
            ?>
            </table>
        </div>

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

</html>

main.js

function deleteConfirm(id) {

    const response = confirm(`Are you sure you want to delete record #${id}?`);

    if(response) {
        document.location.href = "db-deleterecord.php?=id" + id;
    }
}

db-deleterecord.php

<?php
 if(isset($_GET['id'])) {   //  check if "?id=..." exists, i.e. if a GET value id is obtained.
    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 "Deleted record with ID: " .$_GET['id'];
        } else echo "Unable to delete record #" . $_GET['id'] . ": " .$stmt->error;
    } else echo "Unable to prepare statement: " . $conn->error;
 }
 header("refresh:5; url=index.php");
 ?>
myss37ts

myss37ts1#

URL的字符串连接中有错误-

document.location.href = "db-deleterecord.php?=id" + id;

应-

document.location.href = "db-deleterecord.php?id=" + id;

相关问题