How to Delete Multiple Rows Using Checkboxes in PHP: Addressing Common Errors and Ensuring Successful Execution?

Patricia Arquette
Release: 2024-10-26 04:00:27
Original
415 people have browsed it

How to Delete Multiple Rows Using Checkboxes in PHP: Addressing Common Errors and Ensuring Successful Execution?

Delete Multiple Rows by Selecting Checkboxes Using PHP: Troubleshooting Common Errors

When deleting multiple rows from a MySQL database table using PHP, selecting links using checkboxes requires careful coding to ensure successful execution. This code snippet offers a solution to a common issue:

<code class="php"><html>
  <head>
    <title>Links Page</title>
  </head>
  <body>
    <h2>Choose and delete selected links.</h2> 
    <?php
      $dbc = mysqli_connect('localhost','root','admin','sample') or die('Error connecting to MySQL server');
      $query = "select * from links ORDER BY link_id";
      $result = mysqli_query($dbc,$query) or die('Error querying database');
      $count=mysqli_num_rows($result);
    ?>
    <table width="400" border="0" cellspacing="1" cellpadding="0">
      <tr>
        <td>
          <form name="form1" method="post" action="">
            <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
              <tr>
                <td bgcolor="#FFFFFF"> </td>
                <td colspan="3" bgcolor="#FFFFFF">
                  <strong>Delete multiple links</strong>
                </td>
              </tr>
              <tr>
                <td align="center" bgcolor="#FFFFFF">#</td>
                <td align="center" bgcolor="#FFFFFF">
                  <strong>Link ID</strong>
                </td>
                <td align="center" bgcolor="#FFFFFF">
                  <strong>Link Name</strong>
                </td>
                <td align="center" bgcolor="#FFFFFF">
                  <strong>Link URL</strong>
                </td>
              </tr> 
              <?php
              while ($row=mysqli_fetch_array($result)) {
              ?>
              <tr>
                <td align="center" bgcolor="#FFFFFF">
                  <input name="checkbox[]" type="checkbox" value="<?php echo $row['link_id']; ?>">
                </td>
                <td bgcolor="#FFFFFF"> <?php echo $row['link_id']; ?> </td>
                <td bgcolor="#FFFFFF"> <?php echo $row['link_name']; ?> </td>
                <td bgcolor="#FFFFFF"> <?php echo $row['link_url']; ?> </td>
              </tr> 
              <?php
                }
              ?> 
              <tr>
                <td colspan="4" align="center" bgcolor="#FFFFFF">
                  <input name="delete" type="submit" value="Delete">
                </td>
              </tr>
            </table>
          </form>
        </td>
      </tr>
    </table>    
    <?php
      // Check if delete button active, start this
      if(isset($_POST['delete']))
      {
        $checkbox = $_POST['checkbox'];
        for($i=0; $i<count($checkbox); $i++) {
          $del_id = $checkbox[$i];
          $sql = "DELETE FROM links WHERE link_id='$del_id'";
          $result = mysqli_query($dbc, $sql); // Pass the database connection here
        }
        // if successful redirect to view_links.php 
        if($result){
          echo '<meta http-equiv="refresh" content="0;URL=view_links.php">';
        }
      }
      mysqli_close($dbc);
    ?>
  </body>
</html></code>
Copy after login

The fix:

  1. Treat the checkbox name as an array: name="checkbox[]" instead of name="checkbox".
  2. Pass the $dbc database connection to each mysqli_query call, as shown in $result = mysqli_query($dbc, $sql);.

With these modifications, the code should correctly delete multiple rows based on the selected checkboxes.

The above is the detailed content of How to Delete Multiple Rows Using Checkboxes in PHP: Addressing Common Errors and Ensuring Successful Execution?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
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!