如何使用php中的复选框删除多个记录?

时间:2014-04-22 01:37:28

标签: php checkbox sql-delete

我已经在Stack Overflow上检查了几个解决方案,但我似乎无法破解代码。我试图使用复选框删除多个记录,但是当我单击删除按钮时,它不会获得我创建的数组。

<?php


  // Connect to the database
      include("dbconnect.inc");

  // Build SQL statment that selects a student's modules
  $sql = "select * from student;";

  $result = mysql_query($sql,$conn);

      $result = mysql_query($sql,$conn);
  if (isset($_GET['delete']))
 {


 $test = $_GET['test'];

 $i = 0 ;
 $sql= "DELETE FROM student  ";
 foreach ($test as $item_studentid)
{

$i ++;
if ($i == 1)

{

$sql .= "WHERE studentid = " .mysql_real_escape_string($item_studentid)."";
echo "KJ";
}
else
{
$sql.= " OR studentid = "  .mysql_real_escape_string($item_studentid)."";

}

}
$result = mysql_query($sql);
echo $sql;



}
?>

<html>
<head>
<title>Students</title>
<head>
enter code here<body>
<form name="frmStudents" action="" method="post">
    <table border='1'>
    <tr><th colspan='11' align='center'>Students</th></tr>
    <tr><th>ID</th><th>PassCode</th><th>DOB</th><th>First Name</th><th>Surname</th>
    <th>House No.</th><th>Town</th><th>County</th><th>Country</th><th>PostCode</th>                        
   <th>Select</th></tr>
   <?php
  while($row = mysql_fetch_array($result)) 
  {         
    echo "<tr><td> $row[studentid] </td><td> $row[password] </td>";
    echo "<td> $row[dob] </td><td>$row[firstname]</td>";
    echo "<td> $row[lastname] </td><td> $row[house] </td>";
    echo "<td> $row[town] </td><td>$row[county]</td>";
    echo "<td> $row[country] </td><td>$row[postcode]</td>";
    echo "<td> <input type='checkbox' name='test'> </td></tr>";            
   }

 ?>
    </table>

<input name="delete" value="Delete Selected" type="submit" />
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

变化:

echo "<td> <input type='checkbox' name='test'> </td></tr>";            

echo "<td> <input type='checkbox' name='delete[]' value='{$row['studentid']}'></td></tr>"; 

然后您将拥有一个可以处理的$_POST['delete']数组