-1

Can someone please help me, I am new here at PHP programming I want to know how to make a delete button that will Delete all data/values in the table on database This is the table This is my button code This is my delete code Wanted to get help from others this is my button code

<form action="delete.php" method="post">
<input type="submit" name="delete" class="btn btn-danger" value="Clear All Data's" style="float: right;">
</form> 
And this is my delete.php code <?php

$server = "localhost";
$username = "root";
$password = "";
$dbname = "qrcodedb";

$conn = new mysqli($server,$username,$password,$dbname);

if(isset($_POST['delete']))
{
  $name = $_POST['delete'];

  $query = "DELETE FROM table_attendance(NAME,TIMEIN) WHERE name='$name' ";
  $query_run = mysqli_query($conn, $query);

  if($query_run)
  {
    echo '<script> alert("Data Deleted"); </script>';
    header("location:index.php");
  }
    else
  {
    echo '<script> alert("Data Not Deleted"); </script>';
    header("location:index.php");
  }
}
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • just `DELETE FROM table_attendance`; no WHERE, no column list – ysth Dec 12 '22 at 06:44
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Dec 12 '22 at 10:50

1 Answers1

1

If you want to remove all the data from the table then you can use the truncate command.

The TRUNCATE TABLE command deletes the data inside a table, but not the table itself.

"TRUNCATE TABLE `table_attendance`"