如何使用复选框从数据库中删除多个记录并在php中更新

时间:2015-01-06 09:44:37

标签: php mysql database

`             

   ?>
  <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>'

这是我到目前为止所做的代码......但是不知道该怎么做......如果有人知道的话请帮帮我.. !!!

我想删除带有复选框的多个记录,并选择所有删除行和特定行删除

3 个答案:

答案 0 :(得分:2)

  

试试这个

  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">

答案 1 :(得分:0)

将会有圣。像这样:

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
}

答案 2 :(得分:0)

对于HTML。

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

这里我假设您只想删除您的数据,因此您可以创建一个函数,只需通过复选框的形式与news_id建立关系。

它会发生在如下的循环中:

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);
}

请在新代码中don't use mysql_* functions。它们不再维护,并且已被正式弃用。请转而了解prepared statements,并使用PDO]或MySQLi。