Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is it duplicating answers when inserting them into database?
    primarykey
    data
    text
    <p>I am inserting values into a "Question" and "Answer" Table. Now the Question Table is fine as that if I insert 2 questions then the table it displays is below:</p> <pre><code>SessionId (PK) QuestionId(Pk) QuestionContent RZC 1 Name 2 things you will find with a computer RZC 2 Name three things you will find in a toolbox </code></pre> <p>But the "Answer" Table is causing the problem, it duplicates the answers when it inserts, the table currently looks like below:</p> <pre><code>AnswerId (auto PK) SessionId QuestionId Answer 1 RZC 1 A 2 RZC 1 C 3 RZC 2 A 4 RZC 2 B 5 RZC 2 E 6 RZC 1 A 7 RZC 1 C 8 RZC 2 A 9 RZC 2 B 10 RZC 2 E </code></pre> <p>The table should look like below:</p> <pre><code>AnswerId (auto PK) SessionId QuestionId Answer 1 RZC 1 A 2 RZC 1 C 3 RZC 2 A 4 RZC 2 B 5 RZC 2 E </code></pre> <p>Why is it duplicating answers?</p> <p>Below is the php and mysqli code:</p> <pre><code> &lt;?php var_dump($_POST); // Prepare your statements ahead of time $questionsql = 'INSERT INTO Question (SessionId, QuestionId, QuestionContent) VALUES (?, ?, ?)'; if (!$insert = $mysqli-&gt;prepare($questionsql)) { // Handle errors with prepare operation here echo __LINE__.': '.$mysqli-&gt;error; } $answersql = 'INSERT INTO Answer (SessionId, QuestionId, Answer) VALUES (?, ?, ?)'; if (!$insertanswer = $mysqli-&gt;prepare($answersql)) { // Handle errors with prepare operation here echo __LINE__.': '.$mysqli-&gt;error; } //make sure both prepared statements succeeded before proceeding if( $insert &amp;&amp; $insertanswer) { $sessid = $_SESSION['id'] . ($_SESSION['initial_count'] &gt; 1 ? $_SESSION['sessionCount'] : ''); $c = count($_POST['numQuestion']); for($i = 0; $i &lt; $c; $i++ ) { $insert-&gt;bind_param('sis', $sessid, $_POST['numQuestion'][$i], $_POST['questionText'][$i]); $insert-&gt;execute(); if ($insert-&gt;errno) { // Handle query error here echo __LINE__.': '.$insert-&gt;error; break 2; } $results = $_POST['value']; foreach($results as $id =&gt; $value) { $answer = $value; $lastID = $insert-&gt;insert_id; foreach($value as $answer) { $insertanswer-&gt;bind_param('sis', $sessid, $lastID, $answer); $insertanswer-&gt;execute(); if ($insertanswer-&gt;errno) { // Handle query error here echo __LINE__.': '.$insertanswer-&gt;error; break 3; } } } } //close your statements at the end $insertanswer-&gt;close(); $insert-&gt;close(); } ?&gt; </code></pre> <p>The var_dump($_POST) shows this below:</p> <pre><code>array(4) { ["numQuestion"]=&gt; array(2) { [0]=&gt; string(1) "1" [1]=&gt; string(1) "2" } ["questionText"]=&gt; array(2) { [0]=&gt; string(20) "What is 2+2 and 3+3?" [1]=&gt; string(41) "Which three items will you find in a car?" } ["submitDetails"]=&gt; string(14) "Submit Details" ["value"]=&gt; array(2) { [1]=&gt; array(2) { [0]=&gt; string(1) "A" [1]=&gt; string(1) "C" } [2]=&gt; array(3) { [0]=&gt; string(1) "A" [1]=&gt; string(1) "B" [2]=&gt; string(1) "D" } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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