【发布时间】:2015-02-10 22:49:41
【问题描述】:
嘿,我编写此代码是为了从数据库中删除选定的复选框值。是的,我知道这里已经有一些例子。但是当我看着他们并尝试按照他们在他们的问题中所做的事情时。仍然没有获得成功。你们能帮我解决这个问题吗?当我单击删除按钮时,页面刷新并显示记录已成功删除但之后看不到任何记录从数据库中删除的消息
<html>
<head>
<title>
</title>
<meta name="" content="">
</head>
<body bgcolor="#05fa8c">
<form action="a.php" method="POST">
<table style="margin-top: 252px; font-size: larger; font-style: oblique;" align="center" bgcolor="#05fa8c">
<?php
include("config.php"); // Connect to database
//$id = $_POST['id'];
$sbook = "SELECT * FROM `book`";
$rnq = mysqli_query($con,$sbook);
$count = mysqli_num_rows($rnq);
if(!$rnq)
{
print ("Error: %s\n ". mysqli_error($con));
}
echo "<table border=2 table style=margin-top: 252px; font-size: larger; font-style: oblique; bgcolor=#05fa8c>
<tr>
<td width=10% height= 10%><b>Book Name:</b></td>
<td width=10% height= 10%><b>Author Name:</b></td>
<td width=10% height= 10%><b>Publisher Name:</b></td>
<td width=10% height= 10%><b>Description</b></td>
<td width=3% height= 3% align='center'><b>#</b></td>
</tr>";
while($row = mysqli_fetch_array($rnq))
{
$rid = $row['id'];
$bname = $row['bname'];
$aname = $row['aname'];
$pname = $row['pname'];
$desc = $row['description'];
echo "<tr>
<td width=10% height= 10%><b>$bname</b></td>
<td width=10% height= 10%><b>$aname</b></td>
<td width=10% height= 10%><b>$pname</b></td>
<td width=10% height= 10%><b>$desc</b></td>";?>
<td align='center'><input type="checkbox" name="checkbox[<? echo $row['id']; ?>]" id="checkbox[<? echo $row['id']; ?>]" value="<? echo $row['id']; ?>"></td>
<?php } ?>
</table>
<input type='submit' name='delete' id='delete' value='Delete'>
<?php
foreach($_POST['checkbox'] as $del_id){
$del_id = (int)$del_id;
$sql = 'DELETE FROM `book` WHERE `id` = '.$del_id;
mysqli_query($con, $sql);
echo "Records delete successfully";
}
?>
</form>
</body>
</html>
【问题讨论】:
-
在您的输入中尝试
onClick='return false' -
您不能那样使用获取的行。 $del_id = $row['id'];只会得到(也许)最后 $row 的选择!将行数据(来自选择)甚至放在另一个数组中,然后更改 $del_id val
-
尝试自己调试..在for循环中回显$del_id。也许那是空的。检查 $count 是否为空。
-
你的答案在评论部分
foreach,这是一个技巧问题吗? -
不,这实际上不是一个棘手的问题。事实上,我使用所有这些不同的方式来做同样的事情但失败了。我用你建议亚历克斯但没有成功。还在想你想说的话@Marco Mura
标签: php database checkbox mysqli