使用 PDO 插入多行
我在 标签中有一个多复选框,如下所示:
I have a multicheckbox in a <form>
tag like this:
<input type="checkbox" name="del[]" value="<?php echo $menuItems['id']; ?>">
我使用此代码询问此表单:
I ask this form with this code:
if (isset($_POST['subgruppe'])) {
$ids = array();
foreach ($_POST['del'] as $pval) {
$ids[] = (int) $pval;
}
$ids = implode(',', $ids);
echo "groupids";
echo $ids;
echo "userid:";
echo $_POST['userid'];
这向我展示了这样的结果:
This shows me a result like this:
groupids13,9...userid:5
我需要一个给我这样结果的语句:
I need a statment that give me a result like this:
INSERT INTO user_groups (usergroup, userid) VALUE (13,5),(9,5)
...你能给我一个提示我如何检查这个吗?我想我可以解决一个给我的解决方案:(13,5),(9,5)... 变成一个变量.
... Can you give me a hint how i can check this? I think I can handel a solution that give me: (13,5),(9,5)... into a variable.
非常感谢:)
推荐答案
您不必为所有 INSERTS
构建单个字符串,只需在循环时插入即可.
You don't have to build a single string for all of your INSERTS
simply insert while you are looping.
例如:
$sql = "INSERT INTO user_groups (usergroup, userid) VALUE (:usergroup, :userid)";
$stmt = $pdo->prepare($sql);
foreach ($_POST['del'] as $pval) {
$stmt->execute(array(':usergroup'=>(int) $pval,
':userid'=>$_POST['userid']
));
}
相关文章