douxiong3245 2011-04-22 10:01
浏览 136
已采纳

删除复选框选中的记录[复制]

Possible Duplicate:
Delete values selected using checkbox

I want to delete records that has been selected using Checkbox

Checkbox code

<input name="checkbox[]" type="checkbox" value="<?=$row[s_id]?>" id="checkbox[]">

ON delete button

if($_POST['delete'])
{
$cnt=array();
//$cnt=array_count_values($_POST[checkbox]);
$cnt=count($_POST['checkbox']);
for($i=0; $i < $cnt; $i++)
{
        $del_id=$checkbox[$i];
        //$sql = "DELETE FROM t_s_list WHERE `s_id`='".mysql_real_escape_string($del_id)."'";
        $sql = "DELETE * FROM t_s_list WHERE `s_id`= '$del_id'";
        $result = mysql_query($sql);
        mysql_error();
        $NEW="Selected records Deleted";
}
$NEW="Selected records not Deleted";
}
  • 写回答

3条回答 默认 最新

  • duankui6150 2011-04-22 10:41
    关注

    Decided to add my own answer which is basically based on @Sanjay Mohnani's answer.

    if($_POST['delete']) {
        //store the array of checkbox values
        $allCheckBoxId = $_POST['checkbox'];
        //escaping all of them for a MySQL query using array_map
        array_map ('mysql_real_escape_string', $allCheckBoxId);
        //implode will concatenate array values into a string divided by commas
        $ids = implode(",", $allCheckBoxId);
        //building query
        $sql = "DELETE FROM t_s_list WHERE `s_id` IN ($ids)";
        //running query
        mysql_query($sql);
    
        $NEW="Selected records Deleted";
    
    }
    

    Please remember, that whenever you use any value that came from the outside world ($_POST, $_GET, $_COOKIE, etc.) in a MySQL query, escape it beforehand. These values can be easily manipulated by malicious users. This type of attack is called SQL Injection. You can escape values using mysql_real_escape_string().

    If you want to escape all the values in an array, you can use array_map(), which applies a function to all elements of an array.

    You should also be careful when you output something that came from the outside world. For example you should never do echo $_GET['something'];, escape it properly for output (something like echo htmlspecialchars($_GET['something'])).

    Never trust anything that comes from outside.

    One more thing: in an HTML document, an ID can only be used on one element. In your code, all the checkboxes will have the ID checkbox[] which is not a good practice.

    Please do something like:

    <input name="checkbox[]" type="checkbox" value="<?=$row[s_id]?>" id="checkbox_<?=$row[s_id]?>">
    

    which ensures that your elements have a different ID or don't use ID at all. When you post the form, only NAME will be posted, ID is for the client side only (can be used with CSS or Javascript for example).

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

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?