我知道这已经被多次询问了,我已经阅读了很多答案,但是我无法让它发挥作用。我正在尝试创建一个按钮(PHP / HTML)来删除MySQL中的表中的所有记录。我的数据库中有一个名为tvdbase的表。我想删除该表中的所有记录,但保留结构。在任何给定时间,表中的行数不会超过10行。
这是我的代码(我删除了此示例中所有不必要的HTML代码)
<?php include('includes/database.php'); ?>
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
if($_POST){
//Delete records
$query = "DELETE FROM tvdbase";
$mysqli->query($query) or die($mysqli->error.__LINE__);
// Also tried replacing the 2 lines above with:
// mysqli_query( "DELETE FROM tvdbase" );
$msg = "Entries Deleted Successfully";
header('Location:home.php?msg='.urlencode($msg).'');
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-- FORM START -->
<form role="form" method="post" action="delete_all.php">
<!-- BUTTONS START -->
<a class="btn btn-default" href="home.php" role="button">Cancel</a>
<input type="submit" class="btn btn-danger" value="Delete" />
<!-- BUTTONS END -->
</form>
<!-- FORM END -->
</body>
</html>
当我点击删除按钮时,只是重新加载同一页面,数据仍在表格中。我还是PHP和MySQL的新手,所以请原谅我的错误。任何人都可以对我出错的地方有所了解吗?
我对单行有效,我在每行的HTML表格中都有一个删除按钮,代码如下:
<?php include('includes/database.php'); ?>
<?php
//Variable
$id = $_GET['id'];
//Create room select query
$query ="SELECT * FROM tvdbase
WHERE id = $id";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
if($result = $mysqli->query($query)){
//Fetch object array
while($row = $result->fetch_assoc()) {
$tvDate = $row['tvDate'];
$tvCourse = $row['tvCourse'];
$tvRoom = $row['tvRoom'];
}
$result->close();
}
?>
<?php
if($_POST){
//ID Variable
$id = $_GET['id'];
//Delete room
$query = "DELETE FROM tvdbase WHERE id = $id";
$mysqli->query($query) or die($mysqli->error.__LINE__);
// $msg="Entry Deleted";
$msg = "<div class='alert alert-danger'>
Entry Deleted Successfully
</div>";
header('Location:home.php?msg='.urlencode($msg).'');
exit;
}
?>
The form action is delete_room.php?id=<?php echo $id; ?>
答案 0 :(得分:-1)
尝试在IF语句中打印一些内容,以检查代码是否通过。
如果没有,请尝试在表单中添加:
<input type='hidden' name='flag' value='pass'>
然后以这种方式更改您的IF:
if ($_POST['flag'] == "pass") {
}