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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:24:10+00:00 2026-06-12T23:24:10+00:00

I am inserting values into a Question and Answer Table. Now the Question Table

  • 0

I am inserting values into a “Question” and “Answer” Table. Now the Question Table is fine as that if I insert 2 questions then the table it displays is below:

SessionId (PK)  QuestionId(Pk) QuestionContent  

RZC             1              Name 2 things you will find with a computer
RZC             2              Name three things you will find in a toolbox

But the “Answer” Table is causing the problem, it duplicates the answers when it inserts, the table currently looks like below:

AnswerId (auto PK)   SessionId  QuestionId  Answer
1                    RZC         1          A
2                    RZC         1          C
3                    RZC         2          A
4                    RZC         2          B
5                    RZC         2          E
6                    RZC         1          A
7                    RZC         1          C
8                    RZC         2          A
9                    RZC         2          B
10                   RZC         2          E

The table should look like below:

AnswerId (auto PK)   SessionId  QuestionId  Answer
1                    RZC         1          A
2                    RZC         1          C
3                    RZC         2          A
4                    RZC         2          B
5                    RZC         2          E

Why is it duplicating answers?

Below is the php and mysqli code:

   <?php

    var_dump($_POST);  


    // Prepare your statements ahead of time
    $questionsql = 'INSERT INTO Question (SessionId, QuestionId, QuestionContent) VALUES (?, ?, ?)';
    if (!$insert = $mysqli->prepare($questionsql)) {
        // Handle errors with prepare operation here
        echo __LINE__.': '.$mysqli->error;
    }

    $answersql = 'INSERT INTO Answer (SessionId, QuestionId, Answer) 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( $insert && $insertanswer)
            {
                $sessid =  $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '');
                $c = count($_POST['numQuestion']);

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

$insert->bind_param('sis', $sessid, $_POST['numQuestion'][$i], $_POST['questionText'][$i]);

       $insert->execute();

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


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

        $lastID = $insert->insert_id;



        foreach($value as $answer) 
        {
        $insertanswer->bind_param('sis', $sessid, $lastID, $answer);

        $insertanswer->execute();

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

                //close your statements at the end
                $insertanswer->close();
                $insert->close();
            } 
            ?>

The var_dump($_POST) shows this below:

array(4) {
    ["numQuestion"]=> array(2) {
        [0]=> string(1) "1"
        [1]=> string(1) "2"
    }
    ["questionText"]=> array(2) {
        [0]=> string(20) "What is 2+2 and 3+3?"
        [1]=> string(41) "Which three items will you find in a car?"
    }
    ["submitDetails"]=> string(14) "Submit Details" 
    ["value"]=> array(2) {
        [1]=> array(2) {
            [0]=> string(1) "A"
            [1]=> string(1) "C"
        }
        [2]=> array(3) {
            [0]=> string(1) "A"
            [1]=> string(1) "B"
            [2]=> string(1) "D"
        } 
    } 
}
  • 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-12T23:24:11+00:00Added an answer on June 12, 2026 at 11:24 pm

    You have a loop nesting problem. The for, if I get it right, is running the questions and the foreach the answers. The problem is that they should be indipendent from each other. In your code, instead, the foreach is run at every question causing the answers to be inserted twice.

    This should work:

    <?php
    
        var_dump($_POST);  
    
        // Prepare your statements ahead of time
        $questionsql = 'INSERT INTO Question (SessionId, QuestionId, QuestionContent) VALUES (?, ?, ?)';
        if (!$insert = $mysqli->prepare($questionsql)) {
            // Handle errors with prepare operation here
            echo __LINE__.': '.$mysqli->error;
        }
    
        $answersql = 'INSERT INTO Answer (SessionId, QuestionId, Answer) 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($insert && $insertanswer) {
            $sessid =  $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '');
            $c = count($_POST['numQuestion']);
            for($i = 0;  $i < $c; $i++ ) {
                $insert->bind_param('sis', $sessid, $_POST['numQuestion'][$i], $_POST['questionText'][$i]);
                $insert->execute();
                if ($insert->errno) {
                    // Handle query error here
                    echo __LINE__.': '.$insert->error;
                    break 2;
                }   
            }
    
            $results = $_POST['value'];
            foreach($results as $id => $value) {
                $answer = $value;
                $lastID = $insert->insert_id;
    
                foreach($value as $answer) {
                    $insertanswer->bind_param('sis', $sessid, $lastID, $answer);
                    $insertanswer->execute();
    
                    if ($insertanswer->errno) {
                        // Handle query error here
                        echo __LINE__.': '.$insertanswer->error;
                        break 3;
                    }
                }
            }
    
            //close your statements at the end
            $insertanswer->close();
            $insert->close();
        } 
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am inserting values into a table and below is my code protected void
When inserting values into my table what do I edit here? Do I delete
First i gave all values of row in JTAble. While inserting table values into
Hi I'm inserting Latitude, Longitude values into the database. INSERT INTO `Tracks` (`Latitude`, `Longitude`,
I have a question about inserting values into an input field from my MYSQL
I have a simple question. Ive got a trigger to insert the values into
I read this question , which highlights a solution to conditionally insert values into
I am trying to insert question in question table and than getting that latest
Getting error while inserting values into database (SQL Server 2008) Implicit conversion from data
I'm experiencing a problem inserting values into a SQLite database. The data I download

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.