我有两个查询,一个用于填充我的表,另一个用于控制每行的删除按钮。当我在代码中有删除按钮查询时,它会禁用该表并使其为空。关于为什么会发生这种情况的任何想法?
以下是代码:
// Queries
$delquery = "DELETE FROM mods WHERE id = '$id'";
$delete = $conn->query($delquery) or die(mysqli_error($conn));
// Run queries and actions
if (isset($_POST['delete'])) {
echo $delete;
}
// Table population
$query = "SELECT * FROM mods";
$result = $conn->query($query) or die(mysqli_error($conn));
答案 0 :(得分:5)
因为无论条件如何都清空表格。
如果语句,请将前两行代码放在中。
答案 1 :(得分:2)
您的if (isset($_POST['delete'])) {
条件应该包含整个删除查询逻辑 - 尤其是MySQL查询 - 而不仅仅是echo
语句。否则,无论何时加载该代码,您都将始终删除该表。试试这个:
if (isset($_POST['delete'])) {
// Queries
$delquery = "DELETE FROM mods WHERE id = '$id'";
$delete = $conn->query($delquery) or die(mysqli_error($conn));
// Run queries and actions
echo $delete;
}
// Table population
$query = "SELECT * FROM mods";
$result = $conn->query($query) or die(mysqli_error($conn));
我还建议重新处理if
逻辑,这样除了$_POST['delete']
之外array_key_exists
没有设置isset
时,它不会在运行时抛出索引未设置错误{1}}:
if (array_key_exists('delete', $_POST) && isset($_POST['delete'])) {
// Queries
$delquery = "DELETE FROM mods WHERE id = '$id'";
$delete = $conn->query($delquery) or die(mysqli_error($conn));
// Run queries and actions
echo $delete;
}
// Table population
$query = "SELECT * FROM mods";
$result = $conn->query($query) or die(mysqli_error($conn));