Php 如何在提交后使用复选框从数据库中删除数据

Php 如何在提交后使用复选框从数据库中删除数据,php,mysql,checkbox,Php,Mysql,Checkbox,所以我制作了这个网站,我的数据库里有一些数据。php会自动将数据读取到表中。每个表行也会自动获得一个复选框。现在我想选中复选框,然后按delete键,它会将选中的数据从数据库中删除。你知道怎么做吗 这是我的密码: <table width="600" border="1" cellpadding="1" cellspacing="1"> <tr> <th>Username</th> <th>Paswoord</th> <

所以我制作了这个网站,我的数据库里有一些数据。php会自动将数据读取到表中。每个表行也会自动获得一个复选框。现在我想选中复选框,然后按delete键,它会将选中的数据从数据库中删除。你知道怎么做吗

这是我的密码:

<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Username</th>
<th>Paswoord</th>
<th>Delete</th>
</tr>
<?php
while($user = mysql_fetch_assoc($records)){
  echo "<tr>";
  echo "<td>".$user['UserName']."</td>";
  echo "<td>".$user['Pass']."</td>";
  echo "<td> <input type='checkbox' name='checkbox' value='checked'</td>";
  echo "</tr>";
}
?>
</table>
<form method= "POST" action="deleteuser.php">
<input type="submit" name="delete" value="DELETE USERS">
</form>

这就是我一直做的:-

对while循环进行以下更改

<?php
$c=0;
while($user = mysql_fetch_assoc($records)){
  $c++
  echo "<tr>";
  echo "<td>".$user['UserName']."</td>";
  echo "<td>".$user['Pass']."</td>";
  echo "<td> <input type='checkbox' name='checkbox-$c' value='checked'</td>";
  echo "</tr>";

}
echo "<input type='hidden' name='total' value='$c'>";
?>

将复选框的名称从
checkbox
重命名为
checkbox[]
。现在得到的是数组,而不是字符串。 然后将value属性设置为表中您行的ID(主键或需要删除它们的内容)


有两件事需要注意

  • 编写
    标记的语法和正确方式
  • 使用
    中的
    字段
  • 传递一个
    复选框[]
    数组,而不是一个
    复选框
你的代码

用这个,



如果我使用您的回音,可能会重复“输入类型='hidden'name='total'。。。我丢失了我的删除按钮,它不起作用。我想确保在我选中复选框并按下我的删除按钮后,这些项目将从我的列表中删除database@user5227574这不应该发生,我只是更改复选框的
名称。你能把你的文件上传到什么地方吗?这是一个WetTransfer链接,因此您可以下载.zip文件!谢谢!由于安全原因,我无法下载zip文件。将其上载到服务器上。不工作。你的foreachoh是的^ ^ PHP使用
作为
然后在
中使用
-修复我的Artikel,但你还有一个大错误。你必须设置
这就成功了!非常感谢。我可以在foreach循环中与我的数据库建立连接吗?还是这会很糟糕?对于queryNo来说,同样的问题是,最好在一个单独的文件中建立连接,并将其保存为
config.php
,然后像
require('config.php')那样包含/要求该文件在php的开头。好的,最后一个问题。如果我知道要删除链接到复选框的数据库值?我如何做到这一点?对不起,我不能不看代码就说出来,但我的猜测是取一个复选框的值,然后使用该值删除数据库中的记录。类似于
$uname=$user['UserName'];
<?php
$c=0;
while($user = mysql_fetch_assoc($records)){
  $c++
  echo "<tr>";
  echo "<td>".$user['UserName']."</td>";
  echo "<td>".$user['Pass']."</td>";
  echo "<td> <input type='checkbox' name='checkbox-$c' value='checked'</td>";
  echo "</tr>";

}
echo "<input type='hidden' name='total' value='$c'>";
?>
<?php
for($i=1;$i<=$_POST['total'];$i++)
{
    if($_POST['checkbox-$i'] == "checked")
    {
        //commands for delete
    }
}
?>
echo "<td> <input type='checkbox' name='checkbox[]' value='".$user["UserID"]."'</td>";
foreach ($_POST["checkbox"] as $value){
    // ..Delete FROM ... where userID = $value
}
<form method= "POST" action="deleteuser.php">
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Username</th>
<th>Paswoord</th>
<th>Delete</th>
</tr>
<?php
while($user = mysql_fetch_assoc($records)){
  echo "<tr>";
  echo "<td>".$user['UserName']."</td>";
  echo "<td>".$user['Pass']."</td>";
  echo "<td> <input type='checkbox' name='checkbox[]' value='checked'</td>";
  echo "</tr>";
}
?>
</table>
<input type="submit" name="delete" value="DELETE USERS">
</form>
if($_POST['checkbox'] == "checked"){
echo "SUCCEEEEEEES";
}
?>
<?php
    foreach($_POST['checkbox'] as $val)
    {
       echo $val . " this should be deleted";
    }
?>