我有一個$ _POST陣列目前採用以下形式:如何遍歷數組元素,創造多重插入查詢
["Area"]=> array(2) {
[0]=> string(5) "Title"
[1]=> string(5) "Title"
}
["Issue"]=> array(2) {
[0]=> string(3) "111"
[1]=> string(7) "2222222"
}
["Elevation"]=> array(2) {
[0]=> string(8) "11111111"
[1]=> string(7) "2222222"
}
["Fix"]=> array(2) {
[0]=> string(8) "11111111"
[1]=> string(6) "222222"
}
["ExpectFee"]=> array(2) {
[0]=> string(8) "11111111"
[1]=> string(5) "22222"
}
["Outlay"]=> array(2) {
[0]=> string(9) "111111111"
[1]=> string(9) "222222222"
}
["ExpctTime"]=> array(2) {
[0]=> string(9) "111111111"
[1]=> string(11) "22222222222"
}
["Checkbox"]=> array(2) {
[0]=> string(12) "111111111111"
[1]=> string(11) "22222222222"
}
我目前通過它循環像這樣...
if ($_POST['OthProb']['Issue'] != '') {
$table = 'tbl_customproblems';
$kv = array();
foreach ($_POST['OthProb'] as $array) {
foreach ($array as $value) {
$kv[] = "'".$value."'";
}
$string = "INSERT INTO $table (AccountID, myID, Area, Issue, Elevation, Fix, ExpectFee, Outlay, ExpctTime, Checkbox) VALUES ('$_POST[AccountID]', '$_POST[myID]', ".join(", ", $kv).")";
}
} else {
$string = $_SERVER['QUERY_STRING'];
}
$sql = $DBH->prepare($string);
$sql->execute();
哪個幾乎可行!它生產這...
"INSERT INTO tbl_customproblems (AccountID, PropertyID, Area, Issue, Elevation, Fix, ExpectFee, Outlay, ExpctTime, WHCheckbox) VALUES ('81', '81', 'Title', 'Title', '111', '2222222', '11111111', '2222222', '11111111', '222222', '11111111', '22222', '111111111', '222222222', '111111111', '22222222222', '111111111111', '22222222222')"
我如何修改我的循環產生單獨的插入,每行傳遞一。
你濫用'prepare()'。 '$ _POST'值應該放入'execute()'中。而'$ _SERVER ['QUERY_STRING']'是怎麼回事?這不是一個SQL查詢:) –
認爲我是完全自學的。你能提供一個鏈接澄清請,因爲我不知道你剛纔說什麼。 – Funk247
看看['PDO:prepare()'](http://php.net/manual/en/pdo.prepare.php)的例子docs –