它在将值插入数据库时​​复制值

时间:2012-10-14 22:13:18

标签: php database mysqli

我在下面有一个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"
        } 
    } 
}

1 个答案:

答案 0 :(得分: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;