将多个数组提交到mysql数据库php
下面是我正在提交的表格一旦提交,它将显示一份调查问卷。echo语句中隐藏的输入类型包含一个问题id数组。问题文本已打印出来。然后,为要填写的答案创建一个文本框,并将其作为不同答案的数组传递-将多个数组提交到mysql数据库php,php,html,mysql,forms,project,Php,Html,Mysql,Forms,Project,下面是我正在提交的表格一旦提交,它将显示一份调查问卷。echo语句中隐藏的输入类型包含一个问题id数组。问题文本已打印出来。然后,为要填写的答案创建一个文本框,并将其作为不同答案的数组传递-questionanswer[] <form action="addanswers.php" method="post"> <fieldset> <?php $sql = "SELECT *
questionanswer[]
<form action="addanswers.php" method="post">
<fieldset>
<?php
$sql = "SELECT * FROM QUESTIONS WHERE QUESTIONNAIRE_FK = '$questionnaireid';";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
$questionid = $row["QUESTION_ID"];
if($row["QUESTION_TYPE"] == "ff"){
echo "<input type='hidden' name = 'questionid[]' value = '".$questionid."'>".$row["QUESTION_TEXT"]." <input type='text' name='questionanswer[]''></br>";
}
}
?>
<input type="Submit" value="Submit">
</fieldset>
</form>
下面显示addanswers.php。此时此刻,我能够将答案添加到数据库中,该数据库是从问卷中的文本框发布的。我很难将问题id链接到答案文本以将其提交到答案表
$questionanswers = $_POST['questionanswer'];
$questionids = $_POST['questionid'];
foreach($questionanswers as $qa){
$sql = "INSERT INTO ANSWERS (ANSWER_TEXT, QUESTIONNAIRE_FK) values ('$qa', '$questionnaireid')";
mysql_query($sql) or die(mysql_error());
}
在此问题上的任何帮助和建议都将不胜感激。提前感谢您的帮助。好的,我只是想确定您的阵列结构,请尝试以下方法:
$questionanswers = array_map('mysql_real_escape_string', $_POST['questionanswer']);
$questionids = array_map('mysql_real_escape_string', $_POST['questionid']);
foreach($questionanswers as $ind=>$ans){
$sql = "INSERT INTO ANSWERS (ANSWER_TEXT, QUESTIONNAIRE_FK) values ('$ans', '$questionids[$ind]')";
//$ind above contains index of the $questionanswers array-> 0, 1, 2
//since your $questionids index is also numeric and have the
//same number of array values, we can use this index to refer
//the corresponding $questionids
mysql_query($sql) or die(mysql_error());
}
注:
。它们不再得到维护。看到了吗?相反,学习,并使用,或-将帮助您决定哪一个。如果你选择PDO,.你能做一个
print\r
的$\u POST['questionanswer']
和$\u POST['questionid']]
并粘贴在这里吗?数组([0]=>male[1]=>05/12/1991)数组([0]=>1[1]=>2)-当第一个问题是性别,第二个是出生日期时返回。
$questionanswers = array_map('mysql_real_escape_string', $_POST['questionanswer']);
$questionids = array_map('mysql_real_escape_string', $_POST['questionid']);
foreach($questionanswers as $ind=>$ans){
$sql = "INSERT INTO ANSWERS (ANSWER_TEXT, QUESTIONNAIRE_FK) values ('$ans', '$questionids[$ind]')";
//$ind above contains index of the $questionanswers array-> 0, 1, 2
//since your $questionids index is also numeric and have the
//same number of array values, we can use this index to refer
//the corresponding $questionids
mysql_query($sql) or die(mysql_error());
}