Note that there are some explanatory texts on larger screens.

plurals
  1. POforeign keys are not being created
    primarykey
    data
    text
    <blockquote> <p><strong>Question:</strong></p> <p>Want to perform a Select Query below (must be this query):</p> <p>SELECT QuestionId FROM Question WHERE (QuestionNo = ? AND SessionId = ?)</p> <p>In order to be able to find the QuestionId's in the Question table and store it in the Answer Table for all the answers so that we can determine which answers belong to which question</p> </blockquote> <p><strong>Problem:</strong></p> <p>The problem with the mysqli code is that it is not able to insert the correct QuestionId value. It keeps displaying 0 for QuestionId in the <code>Answer</code> Table. So can somebody fix this in order to be able to be able to display the correct QuestionId?</p> <p>It has to be done the SELECT query provided at top. I have to use that in mysqli.</p> <p>Here are the db tables:</p> <p>Question Table</p> <pre><code>QuestionId (auto) SessionId QuestionNo 4 2 1 5 2 2 6 2 3 </code></pre> <p>Answer Table at moment:</p> <pre><code>AnswerId (auto) QuestionId Answer 7 0 A 8 0 C 9 0 A 10 0 B 11 0 True </code></pre> <p>What Answer Table should look like:</p> <pre><code>AnswerId (auto) QuestionId Answer 7 4 A 8 4 C 9 5 A 10 5 B 11 6 True </code></pre> <p>Below is the code:</p> <pre><code> $questionsql = "INSERT INTO Question (SessionId, QuestionNo) VALUES (?, ?)"; if (!$insert = $mysqli-&gt;prepare($questionsql)) { // Handle errors with prepare operation here echo __LINE__.': '.$mysqli-&gt;error; } $answersql = "INSERT INTO Answer (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("ii", $sessionid, $_POST['numQuestion'][$i]); $insert-&gt;execute(); if ($insert-&gt;errno) { // Handle query error here echo __LINE__.': '.$insert-&gt;error; break 1; } } $results = $_POST['value']; foreach($results as $id =&gt; $value) { $answer = $value; $lastID = $id; $questionidquery = "SELECT QuestionId FROM Question WHERE (QuestionNo = ? AND SessionId = ?)"; if (!$questionidstmt = $mysqli-&gt;prepare($questionidquery)) { // Handle errors with prepare operation here echo __LINE__.': '.$mysqli-&gt;error; } // Bind parameter for statement $questionidstmt-&gt;bind_param("ii", $lastID, $sessionId); // Execute the statement $questionidstmt-&gt;execute(); if ($questionidstmt-&gt;errno) { // Handle query error here echo __LINE__.': '.$questionidstmt-&gt;error; break 2; } // This is what matters. With MySQLi you have to bind result fields to // variables before calling fetch() $questionidstmt-&gt;bind_result($quesid); // This populates $optionid $questionidstmt-&gt;fetch(); $questionidstmt-&gt;close(); foreach($value as $answer) { $insertanswer-&gt;bind_param("is", $quesid, $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>
    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.
 

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