Foreach拉多个输入

时间:2013-03-06 05:28:55

标签: php mysql

我有一组输入 - 每个数组有4个问题(范围可以是1-100个键) - 它们可能是也可能不是从1开始(可能像95,96,97,98 ......) 我需要同时从每个键1的所有4个问题中提取输入,不幸的是我无法弄清楚如何... 我熟悉foreach声明,我认为这可能是我最好的选择:

这是我所拥有的

<textarea name="question[98]" rows="3" cols="60"></textarea>
<select name="anstype[98]">
<option value="break">Section Title</option>
...more options
</select>
<input name="d_on[98]" type="text" size="10">
<input name="a_d_on[98]" type="text" size="10">

下一组输入可能是

<textarea name="question[99]" rows="3" cols="60"></textarea>
<select name="anstype[99]">
<option value="break">Section Title</option>
...more options
</select>
<input name="d_on[99]" type="text" size="10">
<input name="a_d_on[99]" type="text" size="10">

理想情况下,我需要将这些内容放入mysql插入语句

$insquery = "INSERT INTO questions (question, anstype, d_on, a_d_on) VALUES('$_POST['question[98]']', '$_POST['anstype[98]']', '$_POST['d_on[98]']', '$_POST['a_d_on[98]']') ";
再次,我不知道关键是什么开始,任何帮助表示赞赏

2 个答案:

答案 0 :(得分:1)

foreach($_POST["question"] as $key=>$value)
{
$question=$value;
$anstype=$_POST["anstype"][$key];
$d_on=$_POST["d_on"][$key];
$a_d_on=$_POST["a_d_on"][$key];

// Run your query here for one complete entry and it will repeat with loop

}

答案 1 :(得分:0)

你需要使用这样的密钥:

$_POST['question'][]

在你的情况下,例如:98:$ _POST ['question'] [98],但最好迭代它。