从php中的表中删除选定的行

时间:2012-09-19 07:12:15

标签: 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.";
        }
    }
>?

8 个答案:

答案 0 :(得分:2)

error_reporting(E_ALL);添加到文件顶部。现在您将能够看到所有错误。 http://php.net/manual/en/function.error-reporting.php

另外,使用var_dump()检查变量中的实际内容。当您运行var_dump($_POST);时,您可以清楚地看到帖子中的实际内容。 http://php.net/manual/en/function.var-dump.php

答案 1 :(得分:1)

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']; ?>">

答案 2 :(得分:0)

代码中有一个Typo输出id:

... value="<? echo $rows['id']; ?>">

应该是

... value="<? echo $row['id']; ?>">

请注意,您的代码也容易受到SQL-injection攻击。您应该检查POST请求中的ID是否真的是整数。

答案 3 :(得分:0)

<? echo $rows['id']; ?>

此处没有$行删除s put

<? echo $row['id']; ?>

答案 4 :(得分:0)

您应该将复选框命名为

name="checkbox[<?php echo $row['id'] ?>]"

然后当你试图删除

foreach ($_POST['checkbox'] as $id => $delete) {
    // delete id
}

你通过计数选择id的方式相当尴尬,你也只删除1行,这显然是不存在的,因为计数不是id。 此外,复选框的值是指勾选或不勾选的复选框,它通常不会保留值,通常的方法是使名称保持复选框实际意味着什么。

答案 5 :(得分:0)

在选择记录之前放置删除代码,然后只能看到数据库中可用的实际记录。

    <?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";
>?

答案 6 :(得分:0)

您应将此代码移至代码

中的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.";
    }
}

答案 7 :(得分:0)

<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>