0

我在下面有一个 php 代码,它在其中插入问题和答案:

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

问题是虽然它给了我这个错误:

警告:mysqli_stmt::execute(): (23000/1062): 第 92 行 /.../ 中键 'PRIMARY' 的重复条目 'RXT-1' 97: 键 'PRIMARY' 的重复条目 'RXT-1'

现在我有“SessionId”和“QuestionId”的复合键,但我不相信问题是这样的,因为下面是表格显示的内容:

问题表:

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

我认为的问题是它将问题 1 中的相同问题显示到两个表行中。问题 2 应该是一个不同的问题(什么是 5+5 和 6+6?)

所以我的问题是为什么它在两行中都显示相同的问题以及如何更改我的代码来修复它?

以下是 var_dump($_POST) 显示的内容:与此问题相关的是["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"
        } 
    } 
}
4

1 回答 1

0

如果你想先插入问题,然后是所有答案,你的问题查询不应该在答案插入循环中,否则它将为找到的每个答案执行,但不会改变。

坏的:

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

更好的:

    $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;
于 2012-10-14T23:01:40.770 回答