How to delete rows using checkboxes in php and mysql-PHP Chinese Network Q&A
How to delete rows using checkboxes in php and mysql
P粉982009874
P粉982009874 2023-09-01 22:02:23
0
1
478

I created a table with the database information and tried creating checkboxes to be able to delete rows more easily, but something isn't working.

I have a button with a form:

I have rows with checkboxes:

Also 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 empty, so nothing is deleted from the database. How can I solve this problem?

P粉982009874
P粉982009874

reply all (1)
P粉006540600

Please note that the data submitted will be within the scope of

....

Since you have two forms, when you click the submit button in the first form, it will not send the second form's data to the server.

So change the second form to:

[Additional explanation]

If you want to stick with the first form to trigger the deletion then please:

  1. In the first form, change delete from "Submit" to "Button"
  2. Add an onclick event to this delete button to trigger the submission of the second form
  3. Make sure there is a hidden field called "delete" in the second form since you specified to include this field in the PHP script
  4. you may have noticed that I have addedid=form2in the 2nd form so as to facilitate triggering of the submission by form1

This is the modified code:

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!