function delete_group($db) {
$ids = Parameters::get('ids');
$ids = implode(',', $ids); // now a string like '5,6,7'.
add_to_log($ids);
try {
$stmt = $db->prepare("DELETE FROM mytable WHERE id IN (:ids)");
$stmt->bindParam(':ids', $ids, PDO::PARAM_STR);
$stmt->execute();
response('success', 'success', NULL);
}
catch (PDOException $e) {
response('error', 'Delete group failed.', NULL);
}
}
此代码不起作用:仅删除第一行。但如果我这样做
$stmt = $db->prepare("DELETE FROM mytable WHERE id IN ($ids)");
相反(只需插入字符串),它可以工作,尽管代码存在 SQL 注入安全问题。如何使其工作并保持安全?