Home  >  Q&A  >  body text

How to delete rows using checkboxes in php and mysql

<p>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. </p> <p>I have a button with a form: </p> <pre class="brush:php;toolbar:false;"><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></pre> <p>I have rows with checkboxes:</p> <pre class="brush:php;toolbar:false;"><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></pre> <p>Also delete-register.php: </p> <pre class="brush:php;toolbar:false;">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; } }</pre> <p>The problem is that "selected" is always empty, so nothing is deleted from the database. How can I solve this problem? </p>
P粉982009874P粉982009874386 days ago522

reply all(1)I'll reply

  • P粉006540600

    P粉0065406002023-09-02 18:45:55

    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 first form, it will not send the second form's data to the server.

    So change the second 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 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 added id=form2 in the 2nd form so as to facilitate triggering of the submission by form1

    This is the modified 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>

    reply
    0
  • Cancelreply