0

我正在使用代码(粘贴在下面)从表中删除它被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.";
        }
    }
>?
4

8 回答 8

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

于 2012-09-19T07:25:47.910 回答
1

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

    <?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";
>?
于 2012-09-19T07:22:16.957 回答
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']; ?>">
于 2012-09-19T07:19:33.160 回答
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>
于 2013-08-06T16:21:55.193 回答
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.";
    }
}
于 2012-09-19T07:55:17.593 回答
0

输出id的代码中有一个错字:

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

它应该是

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

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

于 2012-09-19T07:19:28.720 回答
0
<? echo $rows['id']; ?>

这里没有 $rows 删除 s put

<? echo $row['id']; ?>
于 2012-09-19T07:20:11.480 回答
0

您应该将复选框命名为

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

然后当你试图删除

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

您通过计数选择 id 的方式相当尴尬,您也只删除了 1 行,这显然不存在,因为计数不是 id。此外,复选框的值旨在指示复选框是否被选中,它通常不包含值,通常的方法是使名称包含复选框的实际含义。

于 2012-09-19T07:21:38.620 回答