I am struggling to insert data and be able to select an AnswerId from the db. This is what Im trying to do:
Below is a table I currently have as an example:

I go the details of the above details (except for “Marks Per Answer” column) from the query below:
$query = "SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionContent, an.Answer, q.QuestionMarks
FROM Session s
INNER JOIN Question q ON s.SessionId = q.SessionId
JOIN Answer an ON q.QuestionId = an.QuestionId AND an.SessionId = q.SessionId
WHERE s.SessionName = ?
ORDER BY q.QuestionId, an.Answer
";
Query result:

Each answer from the query result above actually has its own AnswerId in the “Answer” Table which results is below:

Below is the view source code showing how the above table was made:
<table border='1' id='markstbl'>
<thead>
<tr>
<th class='questionth'>Question No.</th>
<th class='questionth'>Question</th>
<th class='answerth'>Answer</th>
<th class='answermarksth'>Marks per Answer</th>
<th class='totalmarksth'>Total Marks</th>
<th class='noofmarksth'>Marks Remaining</th>
</tr>
</thead>
<tbody>
<tr class="questiontd">
<td class="questionnumtd q1_qnum" name="numQuestion" rowspan="3">1 <input type="hidden" name="q1_ans_org" class="q1_ans_org" value="4"><input type="hidden" name="q1_ans" class="q1_ans" value="4"></td>
<td class="questioncontenttd" rowspan="3">Here are 3 answers </td>
<td class="answertd" name="answers[]">B</td>
<td class="answermarkstd">
<input class="individualMarks q1_mark" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="1" onkeypress="return isNumberKey(event)" maxlength="3" />
</td>
<td class="totalmarkstd" rowspan="3">4</td>
<td class="noofmarkstd q1_ans_text" q_group="1" rowspan="3"><strong>4</strong></td>
</tr>
<tr class="questiontd">
<td class="answertd" name="answers[]">D</td>
<td class="answermarkstd">
<input class="individualMarks q1_mark" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="1" onkeypress="return isNumberKey(event)" maxlength="3" />
</td>
</tr>
<tr class="questiontd">
<td class="answertd" name="answers[]">F</td>
<td class="answermarkstd">
<input class="individualMarks q1_mark" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="1" onkeypress="return isNumberKey(event)" maxlength="3" />
</td>
</tr>
<tr class="questiontd">
<td class="questionnumtd q2_qnum" name="numQuestion" rowspan="2">2 <input type="hidden" name="q2_ans_org" class="q2_ans_org" value="6"><input type="hidden" name="q2_ans" class="q2_ans" value="6"></td>
<td class="questioncontenttd" rowspan="2">What 2 speeds can you do in a carriageway (single and dual) </td>
<td class="answertd" name="answers[]">A</td>
<td class="answermarkstd">
<input class="individualMarks q2_mark" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="2" onkeypress="return isNumberKey(event)" maxlength="3" />
</td>
<td class="totalmarkstd" rowspan="2">6</td>
<td class="noofmarkstd q2_ans_text" q_group="1" rowspan="2"><strong>6</strong></td>
</tr>
<tr class="questiontd">
<td class="answertd" name="answers[]">C</td>
<td class="answermarkstd">
<input class="individualMarks q2_mark" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="2" onkeypress="return isNumberKey(event)" maxlength="3" />
</td>
</tr>
</tbody>
</table>
<p>
<input type='hidden' id='num_groups' name='num_groups' value='2'>
<input id="submitBtn" name="submitMarks" type="submit" value="Submit Marks" />
</p>
</form>
The problem I am having with is my insert. I want to insert data into the “Individual_Answer” table into the “AnswerId” and “AnswerMarks” table so we know how many marks each answer is worth. So the database table should look like this below after submission of the top table and insert:
Individual_Answer Table:
AnswerId AnswerMarks
295 2
296 1
297 1
298 3
299 3
I can’t seem to figure out how to first retrieve the “AnswerId” for each answer and then be able to insert the data into the database. Below is my attempt but my question is that can somebody fix the code below in order to be able to retrieve the “AnswerId” and then insert the data correctly?
Below is my attempt (Im using mysqli/php):
<?php
// connect to the database
include('connect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
var_dump($_POST);
$answersql = "INSERT INTO Individual_Answer (AnswerId, AnswerMarks)
VALUES (?, ?)";
if (!$insertanswer = $mysqli->prepare($answersql)) {
// Handle errors with prepare operation here
echo __LINE__.': '.$mysqli->error;
}
//make sure both prepared statements succeeded before proceeding
if($insertanswer)
{
$sessid = $_SESSION['id'] . ($_SESSION['initial_session'] > 1 ? $_SESSION['sessionCounting'] : '');
$c = count($_POST['numQuestion']);
for($i = 0; $i < $c; $i++ )
{
$answerquery = "SELECT AnswerId FROM Answer WHERE (SessionId = ? AND QuestionId = ? and Answer = ?)";
if (!$answerstmt = $mysqli->prepare($answerquery)) {
// Handle errors with prepare operation here
echo __LINE__.': '.$mysqli->error;
}
// Bind parameter for statement
$answerstmt->bind_param("iis", $sessid, $_POST['numQuestion'][$i], $_POST['answers'][$i]);
// Execute the statement
$answerstmt->execute();
if ($answerstmt->errno)
{
// Handle query error here
echo __LINE__.': '.$answerstmt->error;
break 1;
}
// This is what matters. With MySQLi you have to bind result fields to
// variables before calling fetch()
$answerstmt->bind_result($dbAnswerId);
// This populates $optionid
$answerstmt->fetch();
$answersql->bind_param('ii', $dbAnswerId, $_POST['answers'][$i]);
$answerstmt->close();
}
//close your statements at the end
$insertanswer->close();
}
?>
The var_dump($_POST) is displaying this below:
array(7) {
["q1_ans_org"]=> string(1) "4"
["q1_ans"]=> string(1) "0"
["answerMarks"]=> array(5)
{
[0]=> string(1) "2"
[1]=> string(1) "1"
[2]=> string(1) "1"
[3]=> string(1) "3"
[4]=> string(1) "3" }
["q2_ans_org"]=> string(1) "6"
["q2_ans"]=> string(1) "0"
["num_groups"]=> string(1) "2"
["submitMarks"]=> string(12) "Submit Marks"
}
Notice: Undefined index: numQuestion in ... on line 55
You are not binding values to the below query,
Edit: Here’s the manual. It needs to be something like this,