-2

`

   ?>
  <table border="1">
      <tr>
  <th><input type="checkbox" id="selectall"/></th>
  <th style="width:86px;">News ID</th>
 <th style="width:150px;">Title</th>
  <th style="width:590px;"><center>Description</center></th>
 <th style="width:100px;">Status</th>
 <th rowspan="1" colspan="2" style="width:350px"><center>Action </center></th>
 </tr>
 <?php
    while ($rows= mysql_fetch_array($result)) {
?>
<tr>
 <td align="center" bgcolor=""> <input name="checkbox[]" type="checkbox" id="checkbox[]" 
 value="<? echo $rows['news_id']; ?>"> </td>
  <td><center><?php echo $rows["news_id"]; ?>  </center></td>
    enter code here<td><center><?php echo $rows["news_title"]; ?> </center></td>
   <td><center><?php echo $rows["news_content"]; ?></center>  </td>
   <td><center><?php echo $rows["status"]; ?></center> </td>
   <td><center><a href="update_data.php<?php echo $var ?>"><img src="images/update.jpg"  
    height="40px" width="100px" /></a></center> </td>
   <td><center><a href=""><img src="images/delete.jpg" height="40px" width="100px" /></a> 
    </center> </td>
    </tr>
     <?php
       }
         ?>

        <?php
        include('dbconnect.php');
           $del_id = $_POST['checkbox']; 
             $detectinglocations = 'news_id';

           foreach($del_id as $value){
            $sql = "DELETE FROM ".$detectinglocations." WHERE id='".$value."'";
        $result = mysql_query($sql);
               }
           }


          ?>

          ?>
        <tr>
      <td> </td>
   </tr>
          </table>'

This is the code I have done so far... But don't knw what to do ahead.. Help me if anybody knws..!!!

I want to Delete Multiple Records with checkboxes and select all delete rows and particular rows delete

3 Answers3

2

Try This

  if(isset($_POST['delete']))
     {
       for($i=0;$i<count($_POST['checkbox']);$i++)
       {
          $del_id=$_POST['checkbox'][$i];
          $sql = "DELETE FROM register WHERE r_id='$del_id'";
          $result = mysql_query($sql);
       }
       if($result)
       {
         echo "your redirection path";
       }
     }

html

<input name="delete" type="submit" id="delete" value="Delete">
Ravi Sukhadia
  • 443
  • 6
  • 16
0

There will be st. like this:

if (isset($_POST['checkbox'])) { // check if form was submitted and at least one checkbox is checked
    include('dbconnect.php');
    $detectinglocations = 'news_id';

    mysql_query ("DELETE FROM " . $detectinglocations . " 
                  WHERE id IN (" . implode(',', $_POST['checkbox']) . ")"); // delete all checked rows

    header('Location: ...'); // redirect back to the page, unset POST
}
pavel
  • 26,538
  • 10
  • 45
  • 61
0

For HTML.

<input name="checkbox[<?php echo $rows['news_id'];?>]" type="checkbox" id="checkbox" value="1"> 

Here I assume you just want to delete your data, so you can create a function that just does that with a relation to the news_id from the form of checkbox.

It would occur in a loop such as:

foreach($_POST['checkbox'] as $news_id=>$val){
delete_function($news_id,$detectinglocations);
}

function delete_function($news_id,$detectinglocations){
$sql = "DELETE FROM ".$detectinglocations." WHERE id='".$news_id."'";
$result = mysql_query($sql);
}

P.S Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO] or MySQLi.

Community
  • 1
  • 1
Arfan
  • 17
  • 6