我用这样的PHP编写代码,我需要删除数据库中的行,其中删除列中的“删除复选框”已选中。我想将“验证”值(1或0)发送到数据库。但我不知道删除已检查行的SQL命令,我不知道如何获取“验证”列的值(因为无法使用$_POST[$id])
获取值。
<?php
echo "<form action=\"index.php?go=save\" method=\"POST\">
<table width=\"335\" border=\"1\">
<tr>
<th width=\"19\" scope=\"col\">ID</th>
<th width=\"31\" scope=\"col\">Title</th>
<th width=\"33\" scope=\"col\">URL</th>
<th width=\"52\" scope=\"col\">Visitors</th>
<th width=\"28\" scope=\"col\">Hits</th>
<th width=\"52\" scope=\"col\">Verify</th>
<th width=\"27\" scope=\"col\">Edit</th>
<th width=\"41\" scope=\"col\">Delete</th>
</tr>";
$query = mysql_query("SELECT * FROM posts ORDER BY visitors DESC");
while($write = mysql_fetch_array($query)) {
$id = $write['id'];
$title = $write['title'];
$url = $write['url'];
$visitors = $write['visitors'];
$hits = $write['hits'];
echo"
<tr>
<td>$id</td>
<td>$title</td>
<td>$url</td>
<td>$visitors</td>
<td>$hits</td>
<td>
<input type=\"radio\" name=\"$id\" value=\"1\" />Yes
<input type=\"radio\" name=\"$id\" value=\"0\" />No
</td>
<td><a href=\"index.php?go=edit\">Edit</a></td>
<td>
<input type=\"checkbox\" name=\"$id\" />
</td>
</tr>";
}
echo "</table>
<input type=\"submit\" value=\"Save\" />
</form> ";
?>
请帮我解决这个问题。如果您了解其他变体,请写下来。
答案 0 :(得分:0)
删除列:
<input type=\"checkbox\" name=\"delete[]\" value=\"$id\" />
检查和删除:
foreach($_POST['delete'] as $id_to_delete) {
mysql_query("DELETE FROM posts WHERE ID=".$id_to_delete)
}