0

我正在尝试使用复选框删除多行。下面是我的代码

      <?php
$host="localhost"; // Host name 
$username="****"; // Mysql username 
$password="****"; // Mysql password 
$db_name="****"; // Database name 
$tbl_name="****"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$result = mysql_query("SELECT * FROM members WHERE dealer='Panzer Protection'");
?>
<form name="form1" method="post" action="">
      <?php
while($rows=mysql_fetch_array($result)){
?>
      <tr>
        <td bgcolor="#666666"><input name="checkbox[]" type="checkbox" id="checkbox[]"    
value="<? echo $rows['member_id']; ?>"></td>
        <td bgcolor="#666666"><? echo $rows['member_id']; ?></td>
        <td bgcolor="#666666"><center>
          <? echo $rows['member_msisdn']; ?></td>
        <td bgcolor="#666666"><center>
          <? echo $rows['member_name']; ?></td>
        <td bgcolor="#666666"><div align="center"><? echo $rows['dealer']; ?></div>   

 </td>
        <td align="center" bgcolor="#FFFFFF"><a href="control_clientinfo.php?member_id=   
<? echo $rows['member_id']; ?>" class="update">Look Up</a></td>
      </tr>
      <?php
}
?>

<tr>
<td colspan="6" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit"     
id="delete" value="Delete"></td>
</tr>

</form> //Forgot form close in past
<?php

// Check if delete button active, start this 
if($_POST['delete']){
for($i=0;$i<$count;$i++){
$i = 0;
while(list($key, $val) = each($_POST['checkbox'])) {
$sql = "DELETE FROM $tbl_name WHERE id='$val'";
mysql_query($sql);
$i += mysql_affected_rows();
}
}
// if successful redirect to 
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=control_clientlistdel.php\">";
}
}
mysql_close();
?>

它向我显示了我调用的列表,我可以勾选这些框。如果我点击删除按钮,它只会刷新屏幕,我勾选的那个仍然存在

4

2 回答 2

2

第一件事。使用它是个坏主意,mysql因为它真的很旧并且已被弃用。

其次,你在哪里分配你的变量($delete$count

您必须检查delete您的密钥POST是否已设置:

if (isset($_POST['delete'])) { // Then the form has been submitted

在此之后,分配你的$count变量

$checkbox = $_POST['checkbox'];
$count = count($checkbox);

一切都必须正常工作。

最后结果

if (isset($_POST['delete'])) {
    $checkbox = $_POST['checkbox'];
    $count = count($checkbox);

    for($i = 0; $i < $count; $i++) {
        $id = (int) $checkbox[$i]; // Parse your value to integer

        if ($id > 0) { // and check if it's bigger then 0
            mysql_query("DELETE FROM table WHERE member_id = $id");
        }
    }
}

查看用于与数据库交互的mysqliPDO驱动程序。

于 2013-03-24T14:17:22.560 回答
0

不确定它是否是拼写错误..但是您缺少from结束标签并且<table>在发布的代码中..

....
<td colspan="6" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
</form> //here

并且您需要检查$deleteif 条件中的发布值..正确的方法是使用$_POST,因为您使用方法作为 pos.. 此处method="post"

更新

 if(isset($_POST) && $_POST['delete']){ //here
     $count=count($_POST['checkbox']);
    for($i=0;$i<$count;$i++){
      $sql  = "DELETE FROM $tbl_name WHERE id='".$_POST['checkbox'][$i]."'";
      mysql_query($sql);

     }
}

您可以使用header()在 php 中重定向

 header( 'Location: http://www.yoursite.com/ontrol_clientlistdel.php' ) ;
于 2013-03-24T14:05:15.743 回答