php 从php中的表中删除选定的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12490028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete selected row from table in php
提问by swapnil
I am using the code (pasted below) to delete the record from the table when it's selected by the checkbox. It is working, but it is not deleting the record from the table, it is only showing the echo"Records deleted Successfully.".
我正在使用代码(粘贴在下面)从checkbox. 它正在工作,但它没有从表中删除记录,它只显示echo“记录删除成功。”。
Please have a look at my code and suggest me some changes.
请查看我的代码并建议我进行一些更改。
<?php
echo "Hiiiiiiiiii";
include("conn.php");
$sql="select * from test ";
$res=mysql_query($sql) or die(mysql_error());
?>
<form name="form1" method="POST" action="">
<table width="578" border="1" align="center" id="menu">
<tr>
<th></th>
<th>id</th>
<th>Name</th>
<th>email</th>
<th>phno</th>
</tr>
<?php
while($row=mysql_fetch_array($res))
{
?>
<tr>
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['emailid'];?></td>
<td><?php echo $row['phno'];?></td>
<?php
echo"<td><a href='update.php?id=".$row['id']."'>Update</a></td>";
?>
<?php
}
?>
<tr><td><input type="submit" name="delete" value="Delete" id="delete"></td></tr></tr></table>
<?php
// Check if delete button active, start this
$count = mysql_num_rows($res);
echo "$count";
if(isset($_POST['delete']))
{
$delete_id = $_POST['checkbox'];
$id = count($delete_id );
if (count($id) > 0)
{
foreach ($delete_id as $id_d)
{
$sql = "DELETE FROM `test` WHERE id='$id_d'";
$delete = mysql_query($sql);
}
}
if($delete)
{
echo $id." Records deleted Successfully.";
}
}
>?
回答by Jelmer
Add error_reporting(E_ALL);to the top of your file. Now you will be able to see all errors. http://php.net/manual/en/function.error-reporting.php
添加error_reporting(E_ALL);到文件的顶部。现在您将能够看到所有错误。 http://php.net/manual/en/function.error-reporting.php
Also, use var_dump()to check what is actually in your variables. When you run var_dump($_POST);you can clearly see what is actually in your post.
http://php.net/manual/en/function.var-dump.php
此外,用于var_dump()检查变量中的实际内容。当您跑步时,var_dump($_POST);您可以清楚地看到帖子中的实际内容。
http://php.net/manual/en/function.var-dump.php
回答by Mihai Iorga
if(isset($_POST['delete'])){
$delete = false;
$ids = array();
foreach($_POST['checkbox'] as $val){
$ids[] = (int) $val;
}
$ids = implode("','", $ids);
$sql = "DELETE FROM `test` WHERE id IN ('".$ids."')";
$delete = mysql_query($sql);
$id = mysql_affected_rows();
if($delete){
echo $id." Records deleted Successfully.";
}
}
also your check-boxes should have:
你的复选框也应该有:
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['id']; ?>">
回答by Gustonez
<? echo $rows['id']; ?>
there is no $rows here remove the s put
这里没有 $rows 删除 s put
<? echo $row['id']; ?>
回答by fd8s0
You should name the checkboxes something like
您应该将复选框命名为
name="checkbox[<?php echo $row['id'] ?>]"
And then when you're trying to delete
然后当你试图删除
foreach ($_POST['checkbox'] as $id => $delete) {
// delete id
}
Your way of selecting an id by count is rather awkward, you're also only deleting 1 row, which is apparently non-existant because the count is not an id. Also, the value of a checkbox is meant to be an indicator off the checkbox being ticked or not, it's not usually holding a value, the usual way is to make the name hold what the checkbox actually means.
您通过计数选择 id 的方式相当尴尬,您也只删除了 1 行,这显然不存在,因为计数不是 id。此外,复选框的值意味着作为复选框是否被勾选的指示器,它通常不包含值,通常的方法是让名称包含复选框的实际含义。
回答by Arun David
Put the delete code before selecting the records, then only you can see the actual records available in the DB.
在选择记录之前放置删除代码,然后只有您才能看到数据库中可用的实际记录。
<?php
echo "Hiiiiiiiiii";
include("conn.php");
if(isset($_POST['delete']))
{
$delete_id = $_POST['checkbox'];
$id = count($delete_id );
if (count($id) > 0)
{
foreach ($delete_id as $id_d)
{
$sql = "DELETE FROM `test` WHERE id='$id_d'";
$delete = mysql_query($sql);
}
}
if($delete)
{
echo $id." Records deleted Successfully.";
}
}
$sql="select * from test ";
$res=mysql_query($sql) or die(mysql_error());
?>
<form name="form1" method="POST" action="">
<table width="578" border="1" align="center" id="menu">
<tr>
<th></th>
<th>id</th>
<th>Name</th>
<th>email</th>
<th>phno</th>
</tr>
<?php
while($row=mysql_fetch_array($res))
{
?>
<tr>
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['emailid'];?></td>
<td><?php echo $row['phno'];?></td>
<?php
echo"<td><a href='update.php?id=".$row['id']."'>Update</a></td>";
?>
<?php
}
?>
<tr><td><input type="submit" name="delete" value="Delete" id="delete"></td></tr></tr></table>
<?php
// Check if delete button active, start this
$count = mysql_num_rows($res);
echo "$count";
>?
回答by Afshin
you should move this code below of include("conn.php");in your code
你应该将下面的这段代码include("conn.php");在你的代码
if (isset($_POST['delete'])) {
$delete_id = $_POST['checkbox'];
$id = count($delete_id);
if (count($id) > 0) {
foreach ($delete_id as $id_d) {
$sql = "DELETE FROM `test` WHERE id='$id_d'";
$delete = mysql_query($sql);
}
}
if ($delete) {
echo $id . " Records deleted Successfully.";
}
}
回答by Hardik Desai
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id'];?>"></td>
<td><?php echo $row['id'];?></td>
Need to change as below to work it perfectly:
需要进行如下更改才能完美运行:
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="**<?php** echo $rows['id']; ?>"></td>
回答by Sven Koschnicke
There is a Typo in the code that outputs the id:
输出id的代码中有一个Typo:
... value="<? echo $rows['id']; ?>">
It should be
它应该是
... value="<? echo $row['id']; ?>">
Note that your code is also vulnerable to SQL-injectionattacks. You should check if the ids in the POST-request are really integers.
请注意,您的代码也容易受到SQL 注入攻击。您应该检查 POST 请求中的 id 是否真的是整数。

