Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to post and insert values into the database correctly?
    primarykey
    data
    text
    <p>I want use a multi-dimensional array in this format: value[n][], where n is the question number. With this new setup, you should end up with the following checkbox fields:</p> <pre><code>&lt;input type="checkbox" value="A" name="value[1][]"&gt; &lt;input type="checkbox" value="B" name="value[1][]"&gt; &lt;input type="checkbox" value="A" name="value[2][]"&gt; &lt;input type="checkbox" value="C" name="value[2][]"&gt; &lt;input type="checkbox" value="E" name="value[2][]"&gt; </code></pre> <p>Note that the selected value is encoded in the value attribute. The name attribute only contains the question to which the value belongs.</p> <p>So what the above inputs are stating is this:</p> <pre><code>question 1: answer: A question 1: answer: B question 2: answer: A question 2: answer: C question 2: answer: E </code></pre> <p>I want to insert these details into "Question" and "Answer" database tables below:</p> <p>Question Table:</p> <pre><code>SessionId QuestionId MUL 1 MUL 2 </code></pre> <p>Answer Table:</p> <pre><code> AnswerId (auto) SessionId QuestionId Answer 1 MUL 1 A 2 MUL 1 B 3 MUL 2 A 4 MUL 2 C 5 MUL 2 E </code></pre> <p>Now I have attempted writing the mysqli/php code below to insert these values into the database but I am receiving errors and failing badly in wanting to acheive what I want to achieve. I need help being able to correctly insert the correct values in the relevant tables.</p> <p>Below is the php/mysqli code:</p> <pre><code>var_dump($_POST); $i = 0; $c = count($_POST['numQuestion']); for($i = 0; $i &lt; $c; $i++ ){ /* switch ($_POST['gridValues'][$i]){ case "3": $selected_option = "A-C"; break; case "4": $selected_option = "A-D"; break; case "5": $selected_option = "A-E"; break; default: $selected_option = ""; break; } */ needed later on when I insert grid values $results = $_POST['value']; foreach($results as $id =&gt; $value) { $answer = $value; $questionsql = "INSERT INTO Question (SessionId, QuestionId) VALUES (?, ?)"; $sessid = $_SESSION['id'] . ($_SESSION['initial_count'] &gt; 1 ? $_SESSION['sessionCount'] : ''); if (!$insert = $mysqli-&gt;prepare($questionsql)) { // Handle errors with prepare operation here } $insert-&gt;bind_param("si", $sessid, $id); $insert-&gt;execute(); if ($insert-&gt;errno) { // Handle query error here } $insert-&gt;close(); $insert-&gt;insert_id; foreach($value as $answer) { $answersql = "INSERT INTO Answer (SessionId, QuestionId, Answer) VALUES (?, ?, ?)"; if (!$insertanswer = $mysqli-&gt;prepare($answersql)) { // Handle errors with prepare operation here } $insertanswer-&gt;bind_param("sis" $sessid, $lastID, $answer); $insertanswer-&gt;execute(); if ($insertanswer-&gt;errno) { // Handle query error here } $insertanswer-&gt;close(); } } } </code></pre> <p>The <code>var_dump($_POST)</code> outputs this below:</p> <pre><code> array(3) { ["numQuestion"]=&gt; array(2) { [0]=&gt; string(1) "1" [1]=&gt; string(1) "2" } ["submitDetails"]=&gt; string(14) "Submit Details" ["value"]=&gt; array(4) { ["answerARow"]=&gt; string(2) "on" ["answerCRow"]=&gt; string(2) "on" ["answerBRow"]=&gt; string(2) "on" ["answerERow"]=&gt; string(2) "on" } } </code></pre> <p>Below are the errors I am receiving and the line of code each error is linked to:</p> <blockquote> <p>Warning: Invalid argument supplied for foreach() in /.../ on line 252</p> </blockquote> <pre><code>foreach($value as $answer) { </code></pre> <blockquote> <p>Warning: mysqli_stmt::execute(): (23000/1062): Duplicate entry 'MUL-0' for key 'PRIMARY' in /.../ on line 242</p> </blockquote> <p>The above error shows that no question number is being inserted as it keeps displaying it as '0'</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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