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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置