Home >Backend Development >PHP Tutorial >How to Correctly Delete Multiple Rows Using Checkboxes in PHP?

How to Correctly Delete Multiple Rows Using Checkboxes in PHP?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-28 07:23:02500browse

How to Correctly Delete Multiple Rows Using Checkboxes in PHP?

Using PHP to Delete Multiple Rows Selected by Checkboxes

Deleting multiple rows from a database based on user selection is a common task in web development. To accomplish this using checkboxes in PHP, specific steps must be followed.

Code Analysis

The provided code, while attempting to delete multiple rows based on checkbox selection, contains some errors:

  • The checkbox input names are not treated as an array, which prevents proper looping for deletion.
  • No database connection is passed to the query using mysqli_query.

Solution

To resolve these issues and ensure successful row deletion, the following adjustments should be made:

Array-Based Checkbox Names

The checkbox input names should be treated as an array to facilitate looping for deletion. This can be achieved by appending [] to the checkbox name:

<code class="html"><input name="checkbox[]" type="checkbox" value="<?php echo $row['link_id']; ?>"></code>

Database Connection in Query

The database connection ($dbc) should be passed as a parameter to the query using mysqli_query:

<code class="php">$result = mysqli_query($dbc, $sql);</code>

Example Script

The corrected script below incorporates these changes and demonstrates the functionality:

<code class="php"><?php
  // Database connection
  $dbc = mysqli_connect('localhost', 'root', 'admin', 'sample') or die('Error connecting to MySQL server');

  // Query to retrieve records
  $query = "select * from links ORDER BY link_id";
  $result = mysqli_query($dbc, $query) or die('Error querying database');

  // Count records
  $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);
    }

    // If successful, redirect to view_links.php
    if ($result) {
      echo '<meta http-equiv="refresh" content="0;URL=view_links.php">';
    }
  }

  mysqli_close($dbc);
?></code>

By making these corrections, the script will effectively delete multiple rows from the specified table based on the user's checkbox selections.

The above is the detailed content of How to Correctly Delete Multiple Rows Using Checkboxes in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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