dousha7645 2013-03-24 13:53
浏览 12
已采纳

PHP删除带有多个复选框的SQL行

I am trying to delete multiple rows with chekboxes. Below is my code

      <?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();
?>

It shows me the list i call and i can tick the boxes. If i hit delete button it just refreshes the screen and the one i ticked is still there

  • 写回答

2条回答 默认 最新

  • dreamlife2014 2013-03-24 14:17
    关注

    First things first. It's bad idea to use mysql as it is really old and it's deprecated.

    Second, where do you assign your variables ($delete, $count)

    you have to check if the delete key of your POST is set:

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

    after this, assign your $count variable

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

    And everything must work fine.

    Final result

    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");
            }
        }
    }
    

    Check out the mysqli and the PDO drivers for interacting with the database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值