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 8656091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:11:37+00:00 2026-06-12T15:11:37+00:00

JSfiddle: http://jsfiddle.net/ybZvv/57/ I have a fiddle here where the user can append row and

  • 0

JSfiddle: http://jsfiddle.net/ybZvv/57/

I have a fiddle here where the user can append row and select answers for each row. To use the fiddle please follow steps below:

  1. When you open the fiddle, click on the “Add Question” button
    twice. This will append two rows underneath.
  2. In first row select buttons A and B, these buttons will turn green
    and underneath you will see the text input values of these selected
    buttons.
  3. In second row select buttons A, C and E, these buttons will turn
    green and underneath you will see the text input values of these
    selected buttons.

Now what I want to do is that for each answer in each row, they will contain it’s own “AnswerId” and the answers will be inserted into the “Answer” column in the “Answer” table.

Below is what the table should look like following the jsfiddle answers:

Answer Table

AnswerId (auto)    Answer
5                   AB
6                   ACE

The only problem I am getting with the above table is that it is not inserting the answers unser the “Answer” column.

Then I want to store the “AnswerId” from the “Answer” Table in the “Question” table. So as I have appended 2 rows, the “QuestionId” is 1 and 2 and below is what the “Question” Table should look like:

Question Table

QuestionId    AnswerId
1               5
2               6

The above table is working fine.

Below is the error I am recieving:

  • Notice: Undefined offset: 0 in /web/stud/QandATable3.php on line 172

My question is that by looking at the code below, how can the above error be fixed so that it is able to INSERT the answer under the “Answer” column?

Below is the php/mysqli code (I have commented the line where the error is occuring):

    $i = 0;
    $c = count($_POST['gridValues']); //Counts each appended row which works

    for($i = 0;  $i < $c; $i++ ){

 $selected_answer = $_POST['value'];


     $answersql = "INSERT INTO Answer (Answer) 
        VALUES (?)";

          if (!$insertanswer = $mysqli->prepare($answersql)) {
          // Handle errors with prepare operation here
        }  

        $insertanswer->bind_param("s", $selected_answer);

            $insertanswer->execute();

            if ($insertanswer->errno) {
              // Handle query error here
            }

            $insertanswer->close();

            $lastID = $mysqli->insert_id;

    $questionsql = "INSERT INTO Question (QuestionId, AnswerId) 
        VALUES (?, ?)";


        if (!$insert = $mysqli->prepare($questionsql)) {
          // Handle errors with prepare operation here
        }

    $insert->bind_param("ii", $_POST['numQuestion'][$i],  $lastID);

            $insert->execute();

            if ($insert->errno) {
              // Handle query error here
            }

            $insert->close();

    }

    ?>

Below is the code where it successfully appends a question number for each row:

var qnum = 1;

var $qid = $("<td class='qid'></td>").text(qnum);

          $('.num_questions').each( function() {

    var $this = $(this);

     var $questionNumber = $("<input type='hidden' class='num_questionsRow'>").attr('name',$this.attr('name')+"[]")
                   .attr('value',$this.val());

     $qid.append($questionNumber);                             

    ++qnum;    
    $(".questionNum").text(qnum);
    $(".num_questions").val(qnum);

 $tr.append($qid);
  • 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-12T15:11:38+00:00Added an answer on June 12, 2026 at 3:11 pm

    I see issues on both client- and server-sides. I’ll first address the client-side issues.

    The input fields you create have names in the format: value[answerXRow], where X is the selected answer character. If the user selects the same character across multiple answers, there will be multiple input fields with the same name (e.g. two value[answerARow] fields based on your instructions). Moreover, there is no way for your PHP code to tell the difference between which value is for which question, because they will all be submitted in the same value array.

    My suggestion is to 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 input fields:

    <input type="hidden" value="A" name="value[1][]">
    <input type="hidden" value="B" name="value[1][]">
    <input type="hidden" value="A" name="value[2][]">
    <input type="hidden" value="C" name="value[2][]">
    <input type="hidden" value="E" name="value[2][]">
    

    Note that the selected value is encoded in the value attribute. The name attribute only contains the question to which the value belongs.

    Now, on the server-side, you’ll need to iterate over the $_POST['value'] array:

    // $questionNumber is simply n in value[n][]
    // $answers is an array of value attributes from <input>s for this question
    
    foreach ($_POST['value'] as $questionNumber => $answers) {
        // combine all the answers into a single string (e.g. ACE)
        $selected_answer = implode('', $answers);
    
        // continue onto inserting rows into the Answer and Questions tables
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fiddle here: http://jsfiddle.net/ybZvv/11/ The problem I have is that when you
I have a fiddle here: http://jsfiddle.net/ybZvv/43/ The problem I have is with the text
I have two select boxes with dates. See here the jsfiddle: http://jsfiddle.net/egrba/ The people
I have a jsfiddle here: http://jsfiddle.net/ybZvv/22/ The situation I have is that if you
Here is the jsFiddle: http://jsfiddle.net/JFwDw/2/ What I'm wanting to do is use links to
I have some sort of elsif statment bug I can't find: My JSfiddle: http://jsfiddle.net/z3xV3/64/
Here is a jsfiddle http://jsfiddle.net/Jw6kU/2/ of what i have right now. The thing i
I have this jsfiddle http://jsfiddle.net/fwQq4/19/ Here I have the wrapper class ( div.wrapper )
Here is the jsFiddle: http://jsfiddle.net/8dbQu/1/ In brief, I have such structure: <div class='content'> <div
Here is the jsfiddle : http://jsfiddle.net/hhimanshu/sYLRq/ Current Click on Add to list and drop-down

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.