使用按钮删除mysqli数据库中的某些内容?

So I'm just making a simple program that puts names into a database. I got that part down, I can enter a name into a form, then display it on the page, but now I'd like to know how to delete them from the database, and no longer show them on the page.

I added a button next to each name that triggers the third if statement (with the commented out query), and from what I can tell it's best to run a query based on the element's id (my primary key that auto increments), but I have no idea how to get the id from the element who's button I'm clicking on.

How do I get the id from one of the elements in my while loop? Or if there's a better way to delete them, what's that?

if (mysqli_connect_errno()) {
die('could not connect');
}

if (isset($_POST['first_name'], $_POST['last_name'])){
$first_name = trim($_POST['first_name']);
$last_name = trim($_POST['last_name']);
$putitin = mysqli_query($db, "INSERT INTO names (first_name, last_name) VALUES ('$first_name', '$last_name')");
}

if (isset($_POST['del'])){
//$takeitout = mysqli_query($db, "DELETE FROM names WHERE id = ");
}

?>



<html>
<head>
</head>
<body>

<form action='' method='post'>
<div>
<label for "first_name">First name</label>
<input type="text" name="first_name">
</div>
<div>
<label for "last_name">Last name</label>
<input type="text" name="last_name">
</div>
<div>
<input type="submit" value="Insert">
</div>
</form>
<hr>

<?php
$resultset = $db->query('SELECT * FROM names');
if($resultset->num_rows != 0){
while($rows = $resultset->fetch_assoc()) {
    $fname = $rows['first_name'];
    $lname = $rows['last_name'];
    $id = $rows['id'];

    echo "<form action='' method='post'><p>Name: $fname $lname $id<input type='submit' name='del'></form></p>";

}
} else {
echo 'No results';
}

?>

</body>
</html>

This is one way.

change your html part to

<form action='' method='post'>
<input type='hidden' name='id' value='$id' />
<p>Name: $fname $lname $id
<input type='submit' name='del' value=''>
</form></p>

and your php

if (isset($_POST['del'])){
$id = $_POST['id'];
$takeitout = mysqli_query($db, "DELETE FROM names WHERE id = '$id'");
}

Note:

  • What you can do is to put all your input fields inside your while loop. Then assign values to each of them, but we have to use array to store them accordingly.
  • We can use checkbox to store the IDs.
  • What will happen, is user can select from the list of names they wanted to delete by ticking the corresponding checkbox, then pressing the Delete button below.

Your code

<form action="" method="POST">

  <?php
    $resultset = $db->query('SELECT * FROM names');
    if($resultset->num_rows != 0){
    while($rows = $resultset->fetch_assoc()) {
      $fname = $rows['first_name'];
      $lname = $rows['last_name'];
      $id = $rows['id'];

      echo '<input type="checkbox" name="id[]" value="'.$id.'">'.$fname.' '.$lname.'<br>';

    } /* END OF WHILE LOOP */
  ?>
  <input type="submit" value="Delete" name="delete">
</form>

And your PHP that will process the form:

<?php

  if(isset($_POST["delete"])){

    $counter = count($_POST["id"]);
    for($x = 0; $x<$counter; $x++){
      if(!empty($_POST["id"][$x])){ /* CHECK IF AN ITEM IS SELECTED */

        /* DELETE QUERY */
        if($stmt = $db->prepare("DELETE FROM names WHERE id = ?")){
          $stmt->bind_param("i",$_POST["id"][$x]);
          $stmt->execute();
          $stmt->close();
        } /* END OF PREPARED STATEMENT */

      } /* END OF IF; CHECKING IF IT IS SELECTED */
    } /* END OF FOR LOOP */

  } /* END OF ISSET DELETE */

?>