使用复选框删除表单不起作用

时间:2014-11-14 10:07:31

标签: php mysql forms checkbox

我试图获取一个复选框表单来删除数据库/ ftp服务器中的条目,但我无法通过删除按钮删除某些内容。可能是因为我错过了某些内容,因为错误报告一直在告诉变量删除没有确定,但我无法找到原因。

这是我的代码,



<?php error_reporting(E_ALL); ini_set('display_errors', 1);
// Get our database connector
require("includes/conn.php");
?>
<?php
$sql="SELECT * FROM people ORDER BY ID";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Selecteer welke auto('s) verkocht zijn</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Foto</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Merkt/Type</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Beschrijving</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){

echo "<div class=\"picture\">";
                    echo "<p>";
?>

<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo "<img src=\"content/uploads/" . $rows['filename'] . "\" alt=\"\" height=\"125\" width=\"200\" /><br />" . "<br />"; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['fname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lname']; ?></td>
</tr>

<?php 
					echo "</p>";
                    echo "</div>";
}
?>

<tr>
<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($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM people WHERE id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php 
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>

</table>
</form>
</td>
</tr>
</table>
&#13;
&#13;
&#13;

我对该代码的另一个问题是我希望它也能从FTP中删除图像,而不仅仅是数据库中的文件名条目。

P.S。我知道MySQL已经被弃用了,在我让脚本像我想要的那样工作之后,我正在编辑它。没有时间看看PDO和MySQLi。

2 个答案:

答案 0 :(得分:2)

试试这个:

<?php
if(isset($_POST['delete'])){ // Check delete button is clicked
  foreach($_POST['checkbox'] as $del_id){ // loop only checked items and delete
   $sql = "DELETE FROM people WHERE id='$del_id'"; 
   $result = mysql_query($sql);
  }
}
?>

以及您为删除编写的逻辑在您输出所需输出的html代码之前会移到页面顶部。

答案 1 :(得分:0)

您尝试使用变量删除,但未声明。

尝试

<?php

    // Check if delete button active, start this 
    if($_POST['delete']){
       // Delete the record
    }
?>

<form action="" method="post">
<input name="delete" type="submit" id="delete" value="Delete">
</form>