Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9082137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:29:07+00:00 2026-06-16T20:29:07+00:00

I am struggling to insert data and be able to select an AnswerId from

  • 0

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:

enter image description here

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:

enter image description here

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

enter image description here

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:29:09+00:00Added an answer on June 16, 2026 at 8:29 pm

    You are not binding values to the below query,

    $answersql = "INSERT INTO Individual_Answer (AnswerId, AnswerMarks) 
    VALUES (?, ?, ?)";
    

    Edit: Here’s the manual. It needs to be something like this,

    $answersql->bind_param('ii', $value1, $value2); //Value1, value2 are the values to be inserted.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to insert data from an array in mysql. I have multiple
I'm using the statement below to update/insert some data to a table and, if
I have a 9 million rows table and I'm struggling to handle all this
Ive been struggling to get my data to insert into my table. The data
Im struggling with this one. I have a list of items and on a
I have been struggling with this seeminly easy problem for 48 hours, and I
I have a question regarding a unified insert query against tables with different data
While I'm still struggling to find a solution for this question, i have another
Been struggling with this for about 4 hours, I'm attempting to have a modal
I've been struggling to make it work, but cannot pass the inserted data from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.