为什么在将数据插入数据库时​​会重复答案?

时间:2012-10-15 00:12:05

标签: php mysqli

我将值插入“问题”和“答案”表。现在问题表很好,如果我插入2个问题,那么它显示的表格如下:

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

但是“答案”表引起了问题,它在插入时重复了答案,表格目前如下所示:

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

表格如下所示:

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

为什么要重复答案?

下面是php和mysqli代码:

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

var_dump($ _ POST)显示如下:

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 个答案:

答案 0 :(得分:3)

你有一个循环嵌套问题。如果我做对了,那就是运行问题和foreach答案。问题是他们应该彼此独立。相反,在您的代码中,foreach会在每个问题上运行,导致答案被插入两次。

这应该有效:

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

?>