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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:56:50+00:00 2026-06-12T19:56:50+00:00

I have a php code below where it inserts questions and answer: <?php var_dump($_POST);

  • 0

I have a php code below where it inserts questions and answer:

<?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++) {
        switch ($_POST['gridValues'][$i]) {
            case '3':
                $selected_option = 'A-C';
                break;

            case '4':
                $selected_option = 'A-D';
                break;

            case '5':
                $selected_option = 'A-E';
                break;

            default:
                $selected_option = '';
                break;
        }

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

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

            $insert->execute();

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

            $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();
}

Problem is though it is giving me this error:

Warning: mysqli_stmt::execute(): (23000/1062): Duplicate entry
‘RXT-1’ for key ‘PRIMARY’ in /…/ on line 92 97: Duplicate entry
‘RXT-1’ for key ‘PRIMARY’

Now I have composite keys for “SessionId” and “QuestionId” but I don’t believe the problem is this because below is what the table is displaying:

Question Table:

SessionId(PK)  QuestionId(PK)  QuestionContent
RZC            1               What is 2+2 and 3+3?
RZC            2               What is 2+2 and 3+3?

The problem I believe is that it is displaying the same question from question 1 into both table rows. Question 2 should be a different question (What is 5+5 and 6+6?)

So my question is that why is it displaying the same question in both rows and how can I change my code to fix it?

Below is the what the var_dump($_POST) is displaying: The ones associating with this problem are ["numQuestion"] and ["questionText"].

array(8) {
    ["numberAnswer"]=> array(2) {
        [0]=> string(1) "2"
        [1]=> string(1) "3"
    }
    ["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?"
    }
    ["gridValues"]=> array(2) {
        [0]=> string(1) "4"
        [1]=> string(2) "10"
    }
    ["reply"]=> array(2) {
        [0]=> string(8) "multiple"
        [1]=> string(8) "multiple"
    }
    ["textWeight"]=> array(2) {
        [0]=> string(1) "5"
        [1]=> string(1) "5"
    }
    ["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-12T19:56:52+00:00Added an answer on June 12, 2026 at 7:56 pm

    If you want to insert the question first, and after that all answers, your question query shouldn’t be inside the answer insert loop, otherwise it will be executed for every answer found, but not altered.

    Bad:

        $results = $_POST['value'];
        foreach ($results as $id => $value) {
            $answer = $value;
    
            $insert->bind_param('sis', $sessid, $id, $_POST['questionText'][$i]);
    
            $insert->execute();
    
            if ($insert->errno) {
                // Handle query error here
                echo __LINE__ . ': ' . $insert->error;
                break 2;
            }
    

    Better:

        $insert->bind_param('sis', $sessid, $id, $_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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a working php/mysqli code below where it inserts questions and answers successfully:
I have a basic PHP question, take the code below for example, let's say
I have a real basic form (code below) with a bunch of back-panel PhP.
Possible Duplicate: php/Mysql query with inserting date fails I have a datepicker code below:
I have the following *code below, every time I refresh the page it asks
The php code below get's the results from a form and inserts them into
I have a block of code below which inserts data into database using mysqli
I have a mysqli/php code below where it is suppose to insert data into
I have a mysqli/php code below where it is suppose to insert data into
Suppose that I have the PHP code as below: class modual { public $glo;

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.