本文介紹了使用 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']
));
}
這篇關于使用 PDO 插入多行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!