douchuang1852 2016-03-20 11:50
浏览 68
已采纳

从复选框php和mysql中删除多个条目

Ive used the following code to delete and array of checkboxes from another page written in php but I can only delete 1 entry at a time

<?php
    $checked = "";
    $status  = (isset($_REQUEST['status'])); //Checks if any checkboxes have been selected
    if ($status == 'checked') {
        $status  = 1;
        $checked = 'checked="checked"'; 

        $value = $_POST['status']; //Uses the value of the checkbox

        foreach ($value as $val) { //Loops through all of the checkboxes that have been selected
            $sql = "DELETE FROM users WHERE ID='$val'"; //Mysql code to delete from the databases where items have been selected

            if ($conn->query($sql) === TRUE) { //If a value has been deleted 
                echo "Record deleted successfully: "; 
            } else {
                echo "Error deleting record: " . $conn->error; //Prints error if value hasnt deleted
            }
            $conn->close();//Closes sql connection
            echo $val; //Echos values selected
            echo '</br>';
        }
    } else {
        $status = 0;
    }
    echo <<<END
    END;
    ?>    
  • 写回答

1条回答 默认 最新

  • dougua3706 2016-03-20 11:56
    关注

    First and foremost, you're directly inserting posted data into a SQL query — and a DELETE query even! A malicious user can easily exploit this code to delete the entire contents of your database.

    ALWAYS escape user-submitted values that are used in SQL statements.

    But to your direct question: you're calling $conn->close() immediately after executing the first DELETE. By the time you try to do the second, your server connection is already closed. Don't call $conn->close() until after your loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?