Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2012
    Posts
    14

    Unanswered: Deleting multiple rows from mysql table in php using checkboxes

    Hi,
    I am having difficulty deleting rows in my table using check boxes. I have all the check boxes displaying for each row and a delete button below the table. However when i click on the delete button it isnt deleting the checked rows, i have displayed the code below that i am using;
    Code:
     <?php 
    		  
    
    
    
    $result = mysql_query("SELECT * FROM contact ORDER BY msg_id ASC");
    
    echo "<table border='1'>
    <tr>
    <th>Delete</th>
    <th>Message ID</th>
    <th>Name</th>
    <th>Email</th>
    <th>Message</th>
    
    </tr>";
    
    while($row = mysql_fetch_array($result))
      {
    	  ?>
       <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['del_id']; ?>"></td>
       <?php
      echo "<td>" . $row['msg_id'] . "</td>";
      echo "<td>" . $row['name'] . "</td>";
      echo "<td>" . $row['email'] . "</td>";
      echo "<td>" . $row['msg'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";
    ?>
    <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
    </tr>
    
    
    <?php
    // Check if delete button active, start this 
    if(isset($_GET['delete']))	{
    
    for($i=0;$i<$count;$i++){
    $del_id = $checkbox[$i];
    $sql = "DELETE FROM contact WHERE id=".$_GET['del_id'];
    $result = mysql_query($sql);
    }
    
    // if successful redirect to delete_multiple.php 
    if($result){
    
    }
    }
    mysql_close();
    ?>
    I don't quite know what i'm doing wrong
    Any help would be appreciated
    Thanks

  2. #2
    Join Date
    May 2008
    Posts
    277
    Quote Originally Posted by gwg View Post
    PHP Code:
    for($i=0;$i<$count;$i++){
    $del_id $checkbox[$i];
    $sql "DELETE FROM contact WHERE id=".$_GET['del_id'];
    $result mysql_query($sql);

    Assuming you're actually submitting your form using GET (normal/default is POST), take a close look at this and compare what it's actually doing to what you must think it should be doing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •