php - 使用 PHP 中的按钮从数据库中删除记录

标签 php mysql database button

我必须使用按钮从数据库中删除记录,但我的删除查询不起作用。通过插入查询将记录成功输入数据库。我按照 YouTube 上“kanpurwebD”的“如何使用 PHP 和 MySQL 从数据库中删除记录”提供的 php 代码的精确教程进行操作。教程中的代码工作正常,但我的代码仍然没有删除记录。 (我在数据库中输入了 2 条记录)。 我的代码如下:

<div class="container">

<div class="row">
    <form action='add_record.php' method='get'><button type='submit' name='id' value='submit' class='btn btn-default'>ADD RECORD</button><br />
    </form>
    <table class="table table-hover table-responsive">
        <thead>
            <tr>
                <th>Topic #</th>
                <th>Name</th>
                <th>Admin ID</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>
        </thead>

        <tbody>
        <?php
        echo '<br />';
        $query = "SELECT * FROM tb_topic";
        $result = $con->query($query);
        if(isset($_POST['submitDeleteBtn'])){
            $key = $_POST['keyToDelete'];
            $check = "Select * from tb_topic where topic_id = '$key'";
            if(mysqli_num_rows($con, $check)>0){
                $query_delete = mysqli_query($con,"Delete from tb_topic where topic_id = '$key'");
                echo 'record deleted';
            }
        }
        while($query_row = mysqli_fetch_array($result)) {?>

                <tr>
                    <td><?php echo $query_row['topic_id'];?></td>
                    <td><?php echo $query_row['topic_name'];?></td>
                    <td><?php echo $query_row['aid'];?></td>
                    <td><input type = 'checkbox' name = 'keyToDelete' value = "<?php echo $query_row['topic_id'];?>" required></td>
                    <td><input type="submit" name="submitDeleteBtn" class="btn btn-danger"></td>
                </tr>

        <?php }
        ?>

</html>

最佳答案

我通过使用以下语句解决了这个问题:

if(isset($_GET['delete'])) {
    $page = filter_input(INPUT_GET, 'delete', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
    $sql = "DELETE FROM tb_topic WHERE topic_id = $page";
}

$_GET() 没有将 id 作为 int,所以我尝试对其进行类型转换,它对我有用。

关于php - 使用 PHP 中的按钮从数据库中删除记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56734926/

相关文章:

php - Composer,PSR-4 自动加载迭代

JavaScript-jQuery-PHP : Display specific time dynamically in my datetimepicker from the database

mysql - 如何设计我的数据库以容纳这些数据

mysql - 如何将数据库行合并在一起?

java - 数据库负载均衡

php - Neo4Jphp安装和工作初学者

php exec() 响应与 Windows 8 Metro 应用程序不同

php - Mysql 一个 Varchar 列的总和值,它以下列格式将日期保存为月份

mysql - 如何将 Django 与带有复合主键的遗留只读数据库表一起使用?

mysql - 是否可以使用cmd从另一台计算机访问wamp mysql数据库?