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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:51:07+00:00 2026-06-18T05:51:07+00:00

I am trying to insert each answer for each question in the database. Problem

  • 0

I am trying to insert each answer for each question in the database. Problem is that there is a possibility that a question may have no answers, so I tried the code below but it does not insert a db row if a question has no answer, what I was trying to do is that if no answer then display the string No Answer under the Answer column for that question:

 $answersql = "INSERT INTO Answer (QuestionId, Answer) 
    VALUES (?, ?)";
if (!$insertanswer = $mysqli->prepare($answersql)) {
    // Handle errors with prepare operation here
    echo __LINE__.': '.$mysqli->error;
}

if( $insert && $insertanswer)
{

    $c = count($_POST['numQuestion']);
    $question_ids = array();

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

... //Question INSERT goes here

        $questionId = $mysqli->insert_id;

            $question_ids[$questionNo] = $questionId;

}

        $results = $_POST['value'];
        foreach($results as $id => $value) 
        {
            $answer = $value;

            $quesid = (int)$question_ids[$id];   

            foreach($value as $answer) 
            {

            if($answer == '' || $answer === null){
                $answer = 'No Answer';
            }

                $insertanswer->bind_param("is", $quesid, $answer);

                $insertanswer->execute();

                if ($insertanswer->errno) {
                    // Handle query error here
                    echo __LINE__.': '.$insertanswer->error;
                    break 7;
                }
            }
        }


    //close your statements at the end


    $insertanswer->close();

}

The ['value'] comes from an input:

var $newBtn = $(("<input class='answerBtnsRow answers' type='button' style='display:%s;' onclick='btnclick(this, " + gQuestionIndex + ");' />").replace('%s', $this.is(':visible') ? 'inline-block' : 'none')).attr('name', "value[" + gQuestionIndex + "][]").attr('value', $this.val()).attr('class', $this.attr('class')).attr('id', $this.attr('id') + 'Row');

UPDATE:

Below is the SHOW CREATE TABLE for the Answer Table:

CREATE TABLE `Answer` (
 `AnswerId` int(10) NOT NULL AUTO_INCREMENT,
 `QuestionId` int(10) NOT NULL,
 `Answer` varchar(10) DEFAULT NULL,
 PRIMARY KEY (`AnswerId`)
) ENGINE=InnoDB AUTO_INCREMENT=280 DEFAULT CHARSET=utf8

Below is a var dump where if I set question 1 to have answers B and C and set question 2 to have no answers at all, it outputs the following:

var_dump($question_ids);
    var_dump($results);

 array(2) { 
[1]=> int(247) 
[2]=> int(248) 
} 
array(1) { 
[1]=> array(2) { 
[0]=> string(1) "B" 
[1]=> string(1) "C" } 
} 
  • 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-18T05:51:08+00:00Added an answer on June 18, 2026 at 5:51 am

    You can tell from your var_dumps that you have two questions, but only one set of answers. your design flaw doesn’t link the answers to the questions, from what I can see.

    no second index in $results, means no second set of answers, therefore no insert …

    but if you have three questions, and two answer sets, do those answers belong to question 1&2 or 2&3, or 1&3. I am not see how you mach your question id to the result set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem I have is when trying to insert some values ​​in the database
In my bind parameters, I am trying to insert answers for each question. I
I'm new to Obj-C, and I have a little problem here. I'm trying insert
I am trying to insert data into MySQL database. For each of the data
I'm trying to insert XML data into mysql database. Issue I'm having is that
I'm trying to insert a space after each semi-colon, unless the semi-colon is part
I am trying to write out a regex to get each insert line from
I am trying to insert a <br clear=all /> after each fifth div. Below
Trying to insert 315K Gif files into an Oracle 10g database. Everytime I get
I'm trying to insert the current date/time to the database as the last time

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.