dsj60862 2019-06-24 10:45
浏览 473

使用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>
  • 写回答

1条回答 默认 最新

  • dongqiao2077 2019-06-26 05:26
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号