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 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效