您好,我正在尝试使用 php 和 Mysql 进行一项调查,使用下面的代码我保存每个问题的每个答案,但是当我尝试保存 comments_per_questions 时,它没有将该字段保存到数组中,它只保存一个评论。
如何修复此功能以将每个问题的问题、答案和评论保存到数据库中?
提前致谢。
数据库结构
"Questions" (idquestion, question)
"Surveys" (idsurvey, idquestion, answers, comments_per_question, survey_number)
这部分代码保存调查表单中的问题和答案。
public function NewSurveyMulti($answer = array())
{
if(!empty($answer)) {
foreach($answer as $questi => $value ) {
$this->MyDB->Write("INSERT INTO surveys (`idquestion`, `answers`,`comments_per_questions` )
VALUES(
'".$questi."',
'".$value[0]."',
'".$_POST["comment"]."')");
}
}
survey_form.php
<?php
// Fetch questions
$cuestionario = $con->Fetch("SELECT * FROM questions"); ?>
<form name="newDona" action="" method="post">
</table><?php
// Confirm there are questions being drawn from database
$numrows = (is_array($cuestionario))? count($cuestionario): 0;
if($numrows > 0) {
// Loop through questions
foreach($cuestionario as $row) { ?>
<tr>
<!-- Write the question -->
<td><?php echo $row["question"];?></td>
</tr>
<th>
<!-- Set the question id -->
<select name="answer[<?php echo $row['idquestion']; ?>][]">
<option value=""></option>
<option value="1">yes</option>
<option value="no">NO</option>
</select>
</th><?php
<th><textarea type="text" name="comment" maxlength="50" cols="130" rows="5"/ ></textarea></th>
} ?>
<tr>
<td colspan="5" align="center">
<input type="submit" name="send" id="send" value="SAVE" />
</td>
</tr>
</table>
</form>
<?php } ?>
最佳答案
好的,现在问题显示了循环内的注释字段,请执行您对选择所做的操作。
<textarea type="text" name="comment[<?php echo $row['idquestion']; ?>]" maxlength="50" cols="130" rows="5"/ >
并将其收集到循环中
$_POST["comment"][$questi]
旁白:你应该 prevent SQL injection因为您的代码使您的网站容易受到攻击。
关于php - 如何修复我的代码以使用 php、mysql 在数组中保存 3 个值来进行调查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26550415/