dswmmvrg40957 2010-10-26 21:25
浏览 10
已采纳

使用复选框删除多个记录

I am trying to figure out how I can delete multiple records using check boxes. I have a table on my page that gets the data from the database. The first column for each row has a check box that looks like the following:

<input type="checkbox" name="checked[]" value='.$row['UserId'].' class="checkbox" />

My code looks like the following:

<?php
// get required includes
require_once(ROOT_PATH.'connections/mysql.php');
require_once(ROOT_PATH.'admin/controls/users_az/error_messages.php');

// declare variables
$msg = '';

// ------------------------------------------------------------------
// DELETE SELECTED USERS
// ------------------------------------------------------------------
if(isset($_POST['btnDeleteSelected']) && isset($_POST['checked']))
{
$checked = array_map(mysqli_real_escape_string($conn, $_POST['checked']));
$list = "'" . implode("','", $checked) . "'";

$delete_selected = mysqli_query($conn, "DELETE FROM users WHERE UserId IN ($list)")
or die($dataaccess_error);

if($delete_selected)
{
    $msg = mysqli_affected_rows($delete_selected).' '.$msg_success;
}
}
elseif(isset($_POST['btnDeleteSelected']) && !isset($_POST['checked']))
{
$msg = $msg_error;
}
?>

Problem: Naturally this does not work. This is the first time I am attempting to do this.

Question: Am I on the right path with this? How do I need to modify this to make it work?

  • 写回答

3条回答 默认 最新

  • doushantun0614 2010-10-26 21:35
    关注

    This will yield an error:

    $checked = array_map(mysqli_real_escape_string($conn, $_POST['checked']));
    

    Because array_map wants a callback as the first, and an array as the second argument. The rest of the setup is pretty good, but as mysqli_real_escape_string needs a connection, array_map or array_walk aren't that convenient, you might try a normal foreach loop. Alternatively, if the id's are always integers:

    $checked = array_map('intval',$_POST['checked']);
    

    ... and drop the quotes around the values in the implode statement (try to feed mysql integers for integer columns, strings for other (char/date/blob/text) columns).

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

报告相同问题?

悬赏问题

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