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

I have to delete a record from database using a button but my delete query does not work. Records are entered in database successfully with insertion query. I followed exact tutorial for php code available on YouTube "How to delete records from database with PHP & MySQL" by "kanpurwebD". The code in tutorial works fine but my code still does not delete record. (I have 2 records entered in database). My code is as follows:

<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>
dongnei3634
dongnei3634 不,我没有复制粘贴代码。只有教程的帮助,这是我自己的代码。我尝试使用href,但不是首选。我为按钮和删除查询编写了简单的代码,但它也没有用。
7 个月之前 回复
dousou2911
dousou2911 没有人关心kanpurweb.....或任何来源。这是你的问题,你告诉我们你尝试了什么?您只是从某处复制粘贴代码并想要一个解决方案。如果你填写XYZ教程的评论框会更好
7 个月之前 回复
dongqiuqiu4736
dongqiuqiu4736 如果从教程中获得上述代码,请停止使用它。此代码对SQL注入攻击持开放态度。您应该使用参数化预准备语句,而不是直接在您的查询中使用完全未转义的用户数据。目前,任何人都可以很容易地删除tb_topic表中的所有行。
7 个月之前 回复
drutcs7210
drutcs7210 您没有用于删除项目的表单,只有几个无关联的输入。顺便说一句,如果您一次只允许删除单个记录,则该复选框似乎没有必要。
7 个月之前 回复

1个回答

I got it resolved by using following statement:

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() was not taking id as int so I tried typecasting it and it worked for me.

展开翻译

译文



我通过使用以下语句解决了它:</ p>

  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”;
}
</ 代码> </ pre>

$ _ GET()没有将id作为int,所以我尝试对它进行类型转换,它对我有用。</ p>
</ div>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问