请帮助删除表格中的多行。
home.php
<form action="del.php" method="get" enctype="multipart/form-data">
<?php
while($row=mysql_fetch_array($rs))
{
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="need_delete[<?php echo $row['id']; ?>]" type="checkbox" id="checkbox[<?php echo $row['id']; ?>]" value="<?php echo $row['id']; ?>"></td>
<td><?php echo $row['listingtype'];?></td>
<td><?php echo $row['propertyname'];?></td>
<td><?php echo $row['price'];?></td>
<td><?php echo $row['listdate'];?></td>
</tr>
<?php
}
?>
</table>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr></form>
del.php
<?php
include "include/db.php";
$tbl_name='propertylistingbasicinfo';
// Check if delete button active, start this
if ( ! empty($_POST['delete'])) {
foreach ($_POST['need_delete'] as $id => $value) {
$sql = 'DELETE FROM `'.$tbl_name.'` WHERE `id`='.(int)$id;
mysql_query($sql);
}
header('Location: home.php');
}
?>
错误:当我点击删除按钮时,会出现空白屏幕。 并且网址如下所示:http://localhost/realestate/del.php?need_delete%5B2%5D=2&delete=Delete
答案 0 :(得分:1)
你混合使用POST获取GET,将表单方法更改为发布
答案 1 :(得分:1)
您需要在表单中使用类似的内容
<input type="checkbox" name="record[]" value="<?php echo $id ?>">
然后你可以使用implode()函数来连接id并删除它们
$deleted = implode(',',$_POST['record']);
$query = mysql_query("delete from table where id in ('$deleted') ");
没有经过测试但是这个想法。
答案 2 :(得分:1)
问题是您使用method =“get”提交表单,而在del.php中,您正在访问$ _POST中的值。在home.php
上更改方法=“get”到method =“post”答案 3 :(得分:0)
我会将其更改为以下内容......
home.php
:
<form action="del.php" method="post" enctype="multipart/form-data">
<table>
<?php while($row=mysql_fetch_array($rs)) { ?>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name="need_delete[]" value="<?php echo $row['id']; ?>" type="checkbox" id="checkbox[<?php echo $row['id']; ?>]" />
</td>
<td><?php echo $row['listingtype'];?></td>
<td><?php echo $row['propertyname'];?></td>
<td><?php echo $row['price'];?></td>
<td><?php echo $row['listdate'];?></td>
</tr>
<?php } ?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete" />
</td>
</tr>
</table>
del.php:
<?php
//Get the db-file and set the table name
include "include/db.php";
$tbl_name='propertylistingbasicinfo';
// Check if delete button active, start this
if ( !empty($_POST['delete'])) {
//Let's sanitize it
$sanitized_post_ids = array();
foreach($_POST['need_delete'] as $id ) $sanitized_post_ids[] = intval($id);
//Get the ids and add a trailing ",", but remove the last one
$in_str = substr(implode(',', $sanitized_post_ids), 0, -1);
//Build the sql and execute it
$sql = 'DELETE FROM ' . $tbl_name ' WHERE id IN (' . $in_str . ')';
mysql_query($sql);
//Redirect!
header('Location: home.php');
}
?>
希望这对你有用(并且我没有设法添加拼写错误或类似的错误)!