Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From what I understood, you are trying to insert multible rows INTO your database. </p> <p>First, when you use the <code>[]</code> in HTML forms you don't have to declare it as an array in <code>php</code></p> <p>Second, each question will have a question name, answer, A,B,C,D</p> <p>When you trying to create the HTML form you could use this format to keep track the question_id or number.</p> <pre><code>&lt;label&gt;Question&lt;/label&gt;&lt;input type='text' id='textbox1' name='inQuestion[1]' class="form-control"&gt; </code></pre> <p>Then you will be able to retrieve this item as <code>$_POST['inQuestion'][1]</code></p> <p>For Example, if you need 10 questions in your test page you can do it like this:</p> <pre><code>&lt;?php for($i = 0; $i &lt;= 10; $i++) : ?&gt; &lt;form method="post"&gt; &lt;div class="col-md-10"&gt; &lt;label&gt;Question&lt;/label&gt;&lt;input type='text' id='textbox1' name='inQuestion[&lt;?=$i;?&gt;]' class="form-control"&gt; &lt;/div&gt; &lt;div class="col-md-2"&gt; &lt;label&gt;Answer&lt;/label&gt;&lt;input type='text' id='textbox1' name='inAnswer[&lt;?=$i;?&gt;]' class="form-control"&gt; &lt;br&gt; &lt;/div&gt; &lt;div class="col-md-3"&gt; &lt;label&gt;A&lt;/label&gt;&lt;input type='text' id='textbox1' name='inA[&lt;?=$i;?&gt;]' class="form-control"&gt; &lt;/div&gt; &lt;div class="col-md-3"&gt; &lt;label&gt;B&lt;/label&gt;&lt;input type='text' id='textbox1' name='inB[&lt;?=$i;?&gt;]' class="form-control"&gt; &lt;/div&gt; &lt;div class="col-md-3"&gt; &lt;label&gt;C&lt;/label&gt;&lt;input type='text' id='textbox1' name='inC[&lt;?=$i;?&gt;]' class="form-control"&gt; &lt;/div&gt; &lt;div class="col-md-3"&gt; &lt;label&gt;D&lt;/label&gt;&lt;input type='text' id='textbox1' name='inD[&lt;?=$i;?&gt;]' class="form-control"&gt; &lt;br&gt;&lt;br&gt; &lt;/div&gt; &lt;input type="submit" value="go"&gt; &lt;/form&gt; &lt;?php endfor; ?&gt; </code></pre> <p>Also, why do you put the content of the Answers A,B,C,D to an array while you have them is columns in your database. Also use these variables in your query too.</p> <pre><code>$inA = $_POST['inA']; $inB = $_POST['inB']; $inC = $_POST['inC']; $inD = $_POST['inD']; </code></pre> <p>Finally Let's try to insert your values now. </p> <pre><code>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 &lt; $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 } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload