0

I created a table with database information and tried to create checkboxes to be able to delete lines more easily, but something is not working correctly.

I have a button with form:

<form action="delete-register.php" method="post">
   <button type="button" class="btn btn-primary"><span class="fe fe-file-plus fe-12 mr-2"></span>New</button>
   <button type="submit" name="delete" class="btn btn-secondary"><span class="fe fe-trash fe-12 mr-2"></span>Delete</button>
</form>

And I have rows with checkboxes:

<form action="delete-register.php" method="post">
  <td>
     <div class="custom-control custom-checkbox">
       <input type="checkbox" class="custom-control-input" id="<?php echo $row['id']; ?>" name="selected[]" value="<?php echo $row['id']; ?>">
       <label class="custom-control-label" for="<?php echo $row['id']; ?>"></label>
     </div>
  </td>
</form>

And there is delete-register.php:

if (isset($_POST['delete'])) {
  if (isset($_POST['selected'])) {
    foreach ($_POST['selected'] as $id) {
      $query = "DELETE FROM registers WHERE id = $id";
      mysqli_query($conn, $query);
    }

    header('Location: registers.php');
    exit;
  }
}

The problem is that "selected" is always null and so nothing is deleted from the database. How can I solve this problem?

6
  • (1) Is it true that you have the two forms on the same page ? (both actions are delete-register.php) (2) Is it true that after you checked the checkboxes, you click the submit button in the 1st form trying to delete the records ?
    – Ken Lee
    Commented Jan 4, 2023 at 1:08
  • Hello Ken Lee, thank you for your message. Yes it's true.
    – Crespum
    Commented Jan 4, 2023 at 1:14
  • 1
    Does this answer your question? Delete multiple rows by selecting checkboxes using PHP Commented Jan 4, 2023 at 3:12
  • 2
    WARNING: When using mysqli you should be using parameterized queries and bind_param to add any data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put $_POST, $_GET or data of any kind directly into a query, it can be very harmful if someone seeks to exploit your mistake.
    – tadman
    Commented Jan 4, 2023 at 3:16
  • 🐘If you're just getting started with PHP and want to build applications, I'd strongly recommend looking at various development frameworks to see if you can find one that fits your style and needs. They come in various flavours from lightweight like Fat-Free Framework to far more comprehensive like Laravel. These give you concrete examples to work from and guidance on how to write your code and organize your project's files.
    – tadman
    Commented Jan 4, 2023 at 3:16

1 Answer 1

0

Please note that the data submitted will be within the scope of <form>....</form>

Since you have two forms, when you click the submit button in the 1st form, it will not send the data of the 2nd form to the server.

Hence, please change the 2nd form to:

<form action="delete-register.php" method="post">

      <div class="custom-control custom-checkbox">
       <input type="checkbox" class="custom-control-input" id="<?php echo $row['id']; ?>" name="selected[]" value="<?php echo $row['id']; ?>">
       <label class="custom-control-label" for="<?php echo $row['id']; ?>"></label>
     </div>

<input type=submit name=delete>

</form>

[Additional Remark]

If you want to stick to using the 1st form to trigger the delete action, then please:

  1. in the 1st form, change the delete from "submit" to "button"
  2. add an onclick event to this delete button so that it will trigger submission of the 2nd form
  3. make sure you have a hidden field known as "delete" in the 2nd form since you specified to have this field in your PHP script
  4. you may have noticed that I have added id=form2 in the 2nd form so as to facilitate triggering of the submission by form1

So this is the revised code:

<form method="post">
   <button type="button" class="btn btn-primary"><span class="fe fe-file-plus fe-12 mr-2"></span>New</button>

   <button type="button" name="delete" class="btn btn-secondary"
onclick='document.getElementById("form2").submit()';
   ><span class="fe fe-trash fe-12 mr-2"></span>Delete</button>

</form>



<form id=form2 action="delete-register.php" method="post">

   <div class="custom-control custom-checkbox">
       <input type="checkbox" class="custom-control-input" id="<?php echo $row['id']; ?>" name="selected[]" value="<?php echo $row['id']; ?>">
       <label class="custom-control-label" for="<?php echo $row['id']; ?>"></label>
     </div>

<input type=hidden name=delete value="delete">

</form>
8
  • Yes, I have a loop in 2nd form. I didn't know about this interference with the 2 forms, thank you. If I add a new entry it will create multiple entries because of the loop. Is there no way I can use the "delete" button I created on the first form? I wanted to use it, because it is in another corner of the page.
    – Crespum
    Commented Jan 4, 2023 at 1:31
  • please see the "additional remark" in my revised answer to do what you want
    – Ken Lee
    Commented Jan 4, 2023 at 1:39
  • I tried and result: If I choose the checkbox in the first row it delete but if I choose another one other than the first doesn't delete.
    – Crespum
    Commented Jan 4, 2023 at 1:55
  • It should work -- Please try this sandbox : createchhk.com/SOanswers/testSO4Jan2023.php (e.g. select 2 and 3, then click delete and see what happens)
    – Ken Lee
    Commented Jan 4, 2023 at 2:01
  • The sandbox work perfectly.
    – Crespum
    Commented Jan 4, 2023 at 2:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.