我现在有一个表单,它通过一个 for 循环,相应地循环通过我希望它出现的选项的次数。
这是它的代码:
<?php for ($i =1; $i <=20; $i++) { ?>
<tr>
<td><center><input type="text" name="qnsNo[]" class="qnsNo"/></center></td>
<td><input type="text" name="question[]" class="question"/></td>
<td><select name="input[]" class="dropdown">
<option value="Normal text line, no input required*">Normal text line, no input required*</option>
<option value="Multiple choice (only one answer) [Radio Button]">Multiple choice (only one answer) [Radio Button]</option>
<option value="Multiple choice (multiple answers) [Check box]">Multiple choice (multiple answers) [Check box]</option>
<option value="Drop-Down Box">Drop-Down Box</option>
<option value="Comment/Essay Box [Textarea]">Comment/Essay Box [Textarea]</option>
<option value="Single Line Textbox">Single Line Textbox</option>
</select></td>
<td><input type="text" name="options[]" class="option"/></td>
<td><input type="text" name="others[]" class="others"/></td>
<input type="hidden" name="compulsory[]" value=""/>
<td><center><input type="checkbox" name="compulsory[]" value="Compulsory"/></center></td>
</tr>
<?php } ?>
这个表单实际上是允许人们为最后一列“必填字段?”创建自己的表单。允许他们选择这个问题是否是强制性的。但目前我似乎无法将复选框结果正确插入数据库。
我听说如果你想在数据库中插入一个未选中的复选框答案,你可以在实际的复选框行之前放置隐藏字段。我试了运气,插入部分仍然不正确。有什么方法可以插入根据未选中和选中的值?就像如果它未选中,将为'null',如果它被选中,则将插入'Compulsory'......
如果需要,这里是插入数据库的代码:
$sql_array = array();
foreach ($_POST['question'] as $row => $name) {
$question = $name;
$qnsNo = $_POST['qnsNo'][$row];
$input = $_POST['input'][$row];
$options = $_POST['options'][$row];
$others = $_POST['others'][$row];
$compulsory = $_POST['compulsory'][$row];
$idQuery = "SELECT max(surveyID) FROM scSurveyForm WHERE createBy = '$createBy' AND writeUp = '$writeUp'";
$idResult = sqlsrv_query($conn, $idQuery);
$rows = sqlsrv_fetch_array($idResult);
$lastID = $rows[0];
$sql_array[] = "('" . $question . "'," . $lastID . ",'" . $qnsNo . "','" . $input . "','" . $options . "','" . $others . "','" . $compulsory . "')";
if (!empty($question)) {
$query_single = "INSERT INTO scFormLayout(question, surveyID, qnsNo, input, options, others, compulsory)
VALUES" . implode(', ', $sql_array);
echo $query_single.'<br/>';
$status = sqlsrv_query($conn, $query_single);
$sql_array = array();
}
}