我创建了一个数据库,在其中存储了一些数据并将其显示在HTML表格上,我放置了一个删除按钮,但是当我运行脚本删除它而不是刷新它时
我不知道它在哪里失败,这是我的表格代码
<table class="table table-striped table-advance table-hover">
<tbody>
<?php
$conn = mysqli_connect("localhost", "blog", "0000", "blog");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($conn,"SELECT * FROM room_details ");
echo "<tr>
<th> Room Name</th>
<th> Room No.</th>
<th> Type</th>
<th> Price</th>
<th> Action</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['room_name']."</td>";
echo "<td>".$row['room_number']."</td>";
echo "<td>".$row['room_type']."</td>";
echo "<td>".$row['room_price']."</td>";
echo "<td>"?><div class="btn-group">
<a class="btn btn-danger" name="delete"
href="incl/process.php?delete=<?php echo $row['id']; ?>">DELETE<i
class="icon_close_alt2"></i></a>
</div></td>
<?php echo "</tr>";
} ?>
</tbody>
</table>
这是删除处理代码
<?php
$conn = mysqli_connect("localhost", "blog", "0000", "blog");
if(isset($_GET['delete'])) {
$id = $_GET['id'];
$stmt = $mysqli->prepare("DELETE FROM room_details WHERE id = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->close();
header("Refresh: 2; url=rooms.php");
echo '<div class="alert alert-info fade in">
<strong>SUCCESS!!</strong> Data deleted!.
</div>';
}
?>
它不会删除该行,但是现在它挂在process.php上,什么也没有发生,我在哪里出错了?
答案 0 :(得分:0)
不能完全确定为什么它会挂起,但是您尚未设置名为“ id”的变量。应该是:
$id = $_GET['delete'];
答案 1 :(得分:0)
除此之外,您还必须使用$id
而不是$id = $_GET['delete'];
来设置$id = $_GET['id'];
,因为delete
是您在查询字符串中传递的内容,{{1} }方法应在prepare
上调用。
更改此行:
$conn
此行:
$stmt = $mysqli->prepare("DELETE FROM room_details WHERE id = ?");