简体   繁体   English

使用PHP从MySQL表中删除所有行

[英]Delete all rows from MySQL table using PHP

I know this has been asked many times and I have read through a lot of the answers but I cannot get this to work. 我知道这个问题已经被问了很多遍了,我已经阅读了很多答案,但是我无法解决这个问题。 I am trying to create a button (PHP/HTML) to delete all records from a table in MySQL. 我试图创建一个按钮(PHP / HTML)从MySQL的表中删除所有记录。 I have a table in my database called tvdbase. 我的数据库中有一个名为tvdbase的表。 I would like to delete all the records from that table but keep the structure. 我想从该表中删除所有记录,但保留结构。 There will not be more than 10 rows in the table at any given time. 在任何给定时间,表中最多只能有10行。

This is my code (I have removed all the unnecessary HTML code for this example) 这是我的代码(此示例已删除了所有不必要的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> 

When I click the delete button is just reloads the same page and the data is still in the table. 当我单击删除按钮时,只是重新加载同一页面,并且数据仍在表中。 I am still new to PHP and MySQL so please forgive the errors. 我还是PHP和MySQL的新手,请原谅这些错误。 Can anyone shed some light as to where I'm going wrong? 谁能阐明我要去的地方?

I have this working for single rows where I have a delete button in an HTML table on each row with the following code: 我将其用于单行,其中每行的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; ?>

Try to print something in your IF statement, to check if the code pass through. 尝试在IF语句中打印一些内容,以检查代码是否通过。

If not, try adding in your form: 如果没有,请尝试添加您的表单:

<input type='hidden' name='flag' value='pass'>

Then changes your IF in this way: 然后以这种方式更改您的IF:

if ($_POST['flag'] == "pass") {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM