2013-09-28 215 views
0

我正在創建一個多選題測驗,我不知道如何將多個數組插入數據庫。我可以使用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> 

+0

給你完整的代碼,包括HTML代碼。以便我們明白你想要什麼 –

+0

這些數組的值類型是什麼?爲什麼你有一個數組與其中的一個項目? – Othman

+0

你介意給我們更多關於你想要達到什麼的信息嗎? – Othman

回答

0

從我的理解,你想插入multible行到你的數據庫。

首先,當你在HTML表單使用[]您不必將其聲明爲一個數組中php

其次,每個問題都會有一個疑問名字,答案,A,B,C,d

當您嘗試創建HTML表單時,您可以使用此格式來跟蹤question_id或編號。

<label>Question</label><input type='text' id='textbox1' name='inQuestion[1]' class="form-control"> 

然後你就可以檢索該項目作爲$_POST['inQuestion'][1]

例如,如果您需要在您的測試頁10個問題,你可以做這樣的:

<?php for($i = 0; $i <= 10; $i++) : ?> 
<form method="post"> 

    <div class="col-md-10"> 
     <label>Question</label><input type='text' id='textbox1' name='inQuestion[<?=$i;?>]' class="form-control"> 
    </div> 

    <div class="col-md-2"> 
     <label>Answer</label><input type='text' id='textbox1' name='inAnswer[<?=$i;?>]' class="form-control"> 
     <br> 
    </div> 


    <div class="col-md-3"> 
     <label>A</label><input type='text' id='textbox1' name='inA[<?=$i;?>]' class="form-control"> 
    </div> 
    <div class="col-md-3"> 
     <label>B</label><input type='text' id='textbox1' name='inB[<?=$i;?>]' class="form-control"> 
    </div> 
    <div class="col-md-3"> 
     <label>C</label><input type='text' id='textbox1' name='inC[<?=$i;?>]' class="form-control"> 
    </div> 
    <div class="col-md-3"> 
     <label>D</label><input type='text' id='textbox1' name='inD[<?=$i;?>]' class="form-control"> 
     <br><br> 
    </div> 


    <input type="submit" value="go"> 

</form> 

<?php endfor; ?> 

而且,爲什麼你把答案A,B,C,D的內容放到數組中,而你的數據庫中有列。在查詢中也使用這些變量。

$inA = $_POST['inA']; 
$inB = $_POST['inB']; 
$inC = $_POST['inC']; 
$inD = $_POST['inD']; 

最後我們試着現在插入你的值。

if($_POST['btnCreate']){ 

    // The form has been posted! 

    $inLesson = $_POST['inLesson']; 
    $inQuizNo = $_POST['inQuizNo']; 

    $questionsCount = count($_POST['inQuestion']); 

    $items = array(); 

    // Since we should have the same size of arrays. 
    // Also the empty validation has been handled before this step! 
    for($i = 0; $i < $questionsCount; $i++){ 
      $temp = array(); 
     $temp['inQuestion'] = $_POST['inQuestion'][$i]; 
     $temp['inAnswer']  = $_POST['inAnswer'][$i]; 
     $temp['inA']   = $_POST['inA'][$i]; 
     $temp['inB']   = $_POST['inB'][$i]; 
     $temp['inC']   = $_POST['inC'][$i]; 
     $temp['inD']   = $_POST['inD'][$i]; 
      $items[] = $temp; 
    } 


    // Now items should have an array of all questions. 

    foreach($items as $item){ 

     $SQL = 'INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES (
                          "'.$item['inQuestion'].'", 
                          "'.$item['inAnswer'].'", 
                          "'.$item['inA'].'", 
                          "'.$item['inB'].'", 
                          "'.$item['inC'].'", 
                          "'.$item['inD'].'", 
                          "'.$inLesson.'", 
                          "'.$inQuizNo.'")'; 

     $query = mysql_query($SQL); 
    } // End Foreach 
} 
+0

它給了我一個錯誤。這條線上的未定義變量1 >> for($ i = 0; $ i <$$ questionsCount; $ i ++){ – user2825914

+0

我修正了for循環,現在可以試試了。 – Othman

+0

我終於得到了我的答案先生。你是非常大的幫助。謝謝 :) – user2825914

0

嘗試這個

<?php 
    if(isset($_POST['btnCreate'])) { 

     $inQuestion = array($_POST['inQuestion']); 
     $inAnswer = array($_POST['inAnswer']); 
     $inA = $_POST['inA'] ? $_POST['inA'] : null; 
     $inB = $_POST['inB'] ? $_POST['inB'] : null; 
     $inC = $_POST['inC'] ? $_POST['inC'] : null; 
     $inD = $_POST['inD'] ? $_POST['inD'] : null; 
     $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']; 

      $sql = "INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES ('$question', '$answer', '$inA', '$inB', '$inC', '$inD', '$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> 
+0

它仍然沒有工作先生。它給了我一個通知:「在sql語句中對數組進行字符串轉換」。 – user2825914

+0

再次嘗試我的答案,我做了一些編輯到您的HTML和你的PHP代碼 –

0

,而不是發佈一個詳細的解決方案,玩如何插入多行得到一些想法

喜歡的東西:

$_POST['inQuestion'] = array(
    'q1','q2','q3' // from input array name 'inQuestion[]' 
); 

$_POST['inAnswer'] = array(
    'a1','a2','a3' // from input array name 'inAnswer[]' 
); 

$_POST['other'] = 'sunshine'; 

$_POST['yet_another'] = 'nice weather'; 

foreach($_POST['inQuestion'] as $k => $v){ 
$inserts[] = "(".$v.",".$_POST['inAnswer'][$k].",".$_POST['other'].",".$_POST['yet_another'].")"; 
} 

echo "INSERT INTO table col_1, col_2, col_3, col_4 VALUES ".implode(',',$inserts).""; 

// INSERT INTO table col_1, col_2, col_3, col_4 VALUES (q1,a1,sunshine,nice weather),(q2,a2,sunshine,nice weather),(q3,a3,sunshine,nice weather)