尝试使用php中的多个复选框从数据库中删除数据

时间:2015-06-08 17:44:10

标签: php

我正在尝试开发一个管理面板。我需要从雇用列表中删除雇员..我已经尝试过这段代码。但我不能这样做..

错误:

  

未定义的索引复选框..(使用isset解决了这个问题)但我无法从数据库中删除数据。

<?php
include "dbConnect.php";
$display= mysqli_query($con, "SELECT emplys.emp_id,emplys.emp_name,emplys.emp_desig FROM emplys;");
if($display == FALSE){
    echo "no records found";
    }
else{
echo "<div class='tab-pane' id='datatable'>
     <header class='panel-heading'> DataTables <i class='fa fa-info-sign text-muted' data-toggle='tooltip' data-placement='bottom' data-title='ajax to load the data.'></i> </header>
     <div class='table-responsive'> 
     <table class='table table-striped b-t text-sm' data-ride='datatables'> 
     <form action=".$_SERVER['PHP_SELF']." method='post'>
     <input type='button' value='Delete' name='delete'>
     <thead> 
     <tr> 
     <th width='5%'> <input type='checkbox' name='employee_chk' value=""></th> 
    <th class='th-sortable' data-toggle='class'>Id <span class='th-sort'><i class='fa fa-sort-down text'></i><i class='fa fa-sort-up text-active'></i><i class='fa fa-sort'></i> </span></th> 
     <th class='th-sortable' data-toggle='class'>Name <span class='th-sort'><i class='fa fa-sort-down text'></i><i class='fa fa-sort-up text-active'></i><i class='fa fa-sort'></i> </span></th> 
     <th width='30%'>Designation</th> 
     </tr> 
     </thead> <tbody> ";
        while($row = mysqli_fetch_assoc($display)) {
        echo '<tr><td width="5%"> <input name="checkbox[]" type="checkbox" id="checkbox[]" value='.$row["emp_id"].'> </td> <td>'.$row['emp_id'].'</td><td>'.$row['emp_name'].'</td><td>'.$row['emp_desig'].'</td></tr>';
    }
    echo" </tbody> 
      </form>
     </table> 
     </div> 
     </div>";

    if (isset($_POST['delete'])){
    $id=$_POST['checkbox'];
    $N = count($id);
    for($i=0; $i < $N; $i++)
    {
        $result = mysql_query("DELETE FROM emplys where emp_id='$id[$i]'");
    }
    //header("location: view-employ.php");
    }

     }
$con->close();                               ?>

1 个答案:

答案 0 :(得分:0)

<input type='checkbox'>它应该有一个名称,例如<input type='checkbox' name='employee_chk'>

你可以像$_POST['employee_chk']

那样得到它

所以你的代码应该是:

<?php
    include "dbConnect.php";
    $display= mysqli_query($con, "SELECT emplys.emp_id,emplys.emp_name,emplys.emp_desig FROM emplys;");
                                                if($display == FALSE){

                                                    echo "no records found";
                                                }else{
                                                echo "<div class='tab-pane' id='datatable'>
 <header class='panel-heading'> DataTables <i class='fa fa-info-sign text-muted' data-toggle='tooltip' data-placement='bottom' data-title='ajax to load the data.'></i> </header>
 <div class='table-responsive'> 
 <table class='table table-striped b-t text-sm' data-ride='datatables'> 
 <form action='' method='post'>
 <input type='image' src='../images/del.png' width='50' height='50' id='delete' value='Delete' name='delete'>
 <input type='checkbox' name='employee_chk'>
 </form>
 <thead> 
 <tr> 
 <th width='5%'> </th> 
<th class='th-sortable' data-toggle='class'>Id <span class='th-sort'><i class='fa fa-sort-down text'></i><i class='fa fa-sort-up text-active'></i><i class='fa fa-sort'></i> </span></th> 
 <th class='th-sortable' data-toggle='class'>Name <span class='th-sort'><i class='fa fa-sort-down text'></i><i class='fa fa-sort-up text-active'></i><i class='fa fa-sort'></i> </span></th> 
 <th width='30%'>Designation</th> 
 </tr> 
 </thead> <tbody> ";
                                                                            while($row = mysqli_fetch_assoc($display)) {
                                                                            echo '<tr><td width="5%"> <input name="checkbox[]" type="checkbox" id="checkbox[]" value='.$row["emp_id"].'> </td> <td>'.$row['emp_id'].'</td><td>'.$row['emp_name'].'</td><td>'.$row['emp_desig'].'</td></tr>';
                                                                        }
                                                                    echo" </tbody> 
 </table> 
 </div> 
 </div>";


    if(isset($_POST['delete']))
    {   
        $query = "select * from emplys ORDER BY emp_id";
        $result = mysqli_query($con, $query);
        $id= isset($_POST['employee_chk']);
        $N = count($id);

        for($i=0; $i < $N; $i++){

        $del_id = $id[$i];
        $sql = "DELETE FROM emplys WHERE emp_id='$del_id'";
        $result = mysqli_query($con, $sql);
    }
// if successful redirect to view_employ.php 
if($result){
//echo "<meta http-equiv=\"refresh\" content=\"0;URL=view_employ.php\">";
}
 }

                                                $con->close();
                                            }
                                                                                    ?>

<强>更新 <input type='checkbox' name='employee_chk'>应位于form元素内。另请注意,您的复选框不包含任何值。

我认为复选框值必须是employeeID。