我必须使用按钮从数据库中删除记录,但我的删除查询不起作用。通过插入查询将记录成功输入数据库。我按照 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/