[英]Delete selected row from table in php
当checkbox
记录时,我正在使用代码(粘贴在下面)从表中删除记录。 它正在工作,但是没有从表中删除记录,它仅显示echo
“记录已成功删除”。
请查看我的代码,并建议我进行一些更改。
<?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.";
}
}
>?
添加error_reporting(E_ALL);
到文件顶部。 现在您将能够看到所有错误。 http://php.net/manual/en/function.error-reporting.php
另外,使用var_dump()
检查变量中的实际值。 当您运行var_dump($_POST);
您可以清楚地看到帖子中的内容。 http://php.net/manual/zh/function.var-dump.php
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.";
}
}
您的复选框也应具有:
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['id']; ?>">
代码中有一个输入id的错字:
... value="<? echo $rows['id']; ?>">
它应该是
... value="<? echo $row['id']; ?>">
请注意,您的代码也容易受到SQL注入攻击的攻击。 您应该检查POST请求中的ID是否真的是整数。
<? echo $rows['id']; ?>
没有$ rows在这里删除s put
<? echo $row['id']; ?>
您应该将复选框命名为
name="checkbox[<?php echo $row['id'] ?>]"
然后当您尝试删除
foreach ($_POST['checkbox'] as $id => $delete) {
// delete id
}
您通过计数选择ID的方式相当尴尬,您还只删除了1行,由于计数不是ID,因此显然不存在该行。 同样,复选框的值旨在指示复选框是否被选中,它通常不保存值,通常的方法是使名称保留复选框的实际含义。
在选择记录之前放置删除代码,然后只有您可以看到数据库中可用的实际记录。
<?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";
>?
您应该将此代码移到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.";
}
}
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id'];?>"></td>
<td><?php echo $row['id'];?></td>
需要进行以下更改以使其完美工作:
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="**<?php** echo $rows['id']; ?>"></td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.