我正在創建一個多選題測驗,我不知道如何將多個數組插入數據庫。我可以使用array_combine插入前兩個數組,但不能在第3,第4,第5和第6個數組中插入。我可以將問題和答案,但不是選項A,B,C和D插入多個數組到數據庫
我試圖做類似
的foreach(array_combine($ _ POST [ 'inQuestion'],$ _ POST [ 'inAnswer'],$ _POST ['inA'],$ _POST ['inB'],$ _POST ['inC'],$ _POST ['inD'])作爲$ question => $ answer => $ A => $ B => $ C => $ D){
請幫忙。這是我的代碼。
<?php
if(isset($_POST['btnCreate'])) {
$inQuestion = array($_POST['inQuestion']);
$inAnswer = array($_POST['inAnswer']);
$inA = array($_POST['inA']);
$inB = array($_POST['inB']);
$inC = array($_POST['inC']);
$inD = array($_POST['inD']);
$inLesson = $_POST['inLesson'];
$inQuizNo = $_POST['inQuizNo'];
$sql = "SELECT * FROM lessons WHERE title='$inLesson'";
$query = mysql_query ($sql);
$row = mysql_fetch_assoc($query);
$lessonID = $row['lessonID'];
foreach(array_combine($_POST['inQuestion'], $_POST['inAnswer']) as $question => $answer) {
$sql = "INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES ('$question', '$answer', '$A', '$B', '$C', '$D', '$lessonID', $inQuizNo)";
$query = mysql_query($sql);
}
}
?>
<div class="panel panel-info">
<div class="panel-heading">
<h4>Create Assessment - Multiple Choice</h4>
</div>
<div class="panel-body">
<form method="post">
<br>
<div>
<div class="col-md-4">
<strong>Lesson</strong><br>
<select class="form-control" name="inLesson">
<option></option>
<option disabled></option>
<?php
$sql2 = "SELECT * FROM lessons WHERE courseID='$_GET[courseID]'";
$query2 = mysql_query ($sql2);
while ($row2 = mysql_fetch_assoc($query2)) {
?>
<option><?php echo $row2['title']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-4">
<strong>Quiz No.</strong><br>
<input required type="number" class="form-control" name="inQuizNo" min="1" max="100">
</div>
<div class="col-md-4">
<div class="pull-right">
<br>
<input type="button" value="Add" id="addButton" class="btn btn-info">
<input type="button" value="Remove" id="removeButton" class="btn btn-warning">
</div>
</div>
</div>
<br><br><br><br><br>
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1" class="form-group">
<h2><span class="label label-primary">No. 1</span></h2>
<br><br>
<div class="col-md-10">
<label>Question</label><input type='text' id='textbox1' name='inQuestion[]' class="form-control">
</div>
<div class="col-md-2">
<label>Answer</label><input type='text' id='textbox1' name='inAnswer[]' class="form-control">
<br>
</div>
<div class="col-md-3">
<label>A</label><input type='text' id='textbox1' name='inA[]' class="form-control">
</div>
<div class="col-md-3">
<label>B</label><input type='text' id='textbox1' name='inB[]' class="form-control">
</div>
<div class="col-md-3">
<label>C</label><input type='text' id='textbox1' name='inC[]' class="form-control">
</div>
<div class="col-md-3">
<label>D</label><input type='text' id='textbox1' name='inD[]' class="form-control">
<br><br>
</div>
</div>
</div>
<div class="col-md-12">
<button type="submit" name="btnCreate" class="btn btn-success pull-right">Create Quiz
</div>
</form>
</div>
給你完整的代碼,包括HTML代碼。以便我們明白你想要什麼 –
這些數組的值類型是什麼?爲什麼你有一個數組與其中的一個項目? – Othman
你介意給我們更多關於你想要達到什麼的信息嗎? – Othman