ID および Details フィールドを渡すフォームからクエリを作成しようとしています。投稿配列は次のようになります。
array(2) {
["ID"]=> array(9)
{
[0]=> string(3) "10a"
[1]=> string(3) "10b"
[2]=> string(3) "10c"
[3]=> string(3) "10d"
[4]=> string(3) "10e"
[5]=> string(3) "10f"
}
["Details"]=> array(9)
{
[0]=> string(19) "This is textfield 1"
[1]=> string(17) "This is textfield 2"
[2]=> string(0) ""
[3]=> string(0) ""
[4]=> string(0) ""
[5]=> string(0) ""
}
}
ID はページにハードコードされ、常に渡されます。詳細は必要に応じてユーザーが入力できます。
if ($_POST['OthProb']['Details'] != '') {
// Catch issue text and assign to variable
foreach ( $_POST['OthProb']['Details'] as $key => $value) {
$id = $_POST['OthProb']['ID']->$key;
$dets = $value;
// Focus on fields with values and join the array values together in a query string
if ($dets != ''){
$string = "INSERT into $table (AccountID, OtherID, ID, Details) VALUES ($_POST[AccountID], $_POST[OtherID], '".$id.", '".$dets."')";
$sql = $DBH->prepare($string);
// $sql->execute();
// var_dump ($id);
}
}
}
以下を作成します
"INSERT into tbl_problems (AccountID, OtherID, ID, Details) VALUES (80, 80, '10f, 'This is Title issue')"
「詳細」は正しく出力されていますが、ID から最後の ID まで循環しています。これは、foreach をネストしているためですか? ループを構成する最良の方法は何ですか?