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

时间:2019-06-24 10:45:14

标签: php mysql database button

我必须使用按钮从数据库中删除记录,但是删除查询不起作用。记录通过插入查询成功输入数据库。我遵循了YouTube上可用的php代码的确切教程“如何用PHP和MySQL从数据库中删除记录”(由kanpurwebD撰写)。教程中的代码工作正常,但我的代码仍然不会删除记录。 (我在数据库中输入了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>

1 个答案:

答案 0 :(得分:0)

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

 <ion-card class='sc-ion-card-md-h'>
      <ion-card-header>
        <ion-card-subtitle>Name:</ion-card-subtitle>
        <ion-card-title>
          <ion-select #inputName id="username" class="form-control">
            <ion-select-option>SELECT</ion-select-option>
            <ion-select-option>Monicka</ion-select-option>
            <ion-select-option>Hema</ion-select-option>
            <ion-select-option>Ramesh</ion-select-option>
            <ion-select-option>Madhavan</ion-select-option>
            <ion-select-option>Aadhavan</ion-select-option>
            <ion-select-option>Madhan</ion-select-option>
            <ion-select-option>Prasanth</ion-select-option>
          </ion-select>
        </ion-card-title>
      </ion-card-header>
      <ion-card-header>
        <ion-card-subtitle>Date:</ion-card-subtitle>
        <ion-card-title>
          <ion-input type="text" #inputDate id="date" class="form-control"></ion-input>
        </ion-card-title>
      </ion-card-header>
    </ion-card>

$ _ GET()没有将id当作int,所以我尝试进行类型转换,并且对我有用。