在php中添加记录时未将正确的数据插入数据库

时间:2019-03-14 11:03:15

标签: php mysql mysqli

试图将记录插入数据库中,但是数据没有正确插入数据库表中。

这是数据库表:

问题数据库表:

question_number  question_no  text  status
1                 1           html    1
2                 2           php     0

选择数据库表:

id   question_number   is_correct  text   status
1       1                1        abc       1
2       1                0        abc       1
3       1                0        abc       1
4       1                0        abc       1 
5       2                0        abcdfd    0
6       2                1        abcdsf    0
7       2                0        abcdsf    0
8       2                0        abcsdfg   0

如果我要添加新记录,它将以这种格式插入数据库表中

问题表:

question_number  question_no  text  status
3                2           html    1

选择数据库表:

id   question_number   is_correct  text          status
9        2             1           abchdfgh        1
10       2             0           abfdhdfc        1
11       2             0           afdhgbc         1
12       2             0           adfhbc          1 

这是在选项表中插入问题编号为2的问题,但此处应插入为3。

这是代码:

<?php
if(isset($_POST['submit'])){
    //Get post variables
    $question_number = $_POST['question_no'];
    $question_text = $_POST['question_text'];
    $correct_choice = $_POST['correct_choice'];
    //Choices array
    $choices = array();
    $choices[1] = $_POST['choice1'];
    $choices[2] = $_POST['choice2'];
    $choices[3] = $_POST['choice3'];
    $choices[4] = $_POST['choice4'];
    $choices[5] = $_POST['choice5'];

    //Question query
    $query = "INSERT INTO `questions`(question_no, text)
                VALUES('$question_number','$question_text')";
            //print_r($query);
            //die();                
    //Run query
    $insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);

    //Validate insert
    if($insert_row){
        foreach($choices as $choice => $value){
            if($value != ''){
                if($correct_choice == $choice){
                    $is_correct = 1;
                } else {
                    $is_correct = 0;
                }
                //Choice query
                $query = "INSERT INTO `choices` (question_number, is_correct, text)
                        VALUES ('$question_number','$is_correct','$value')";

                //Run query
                $insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);

                //Validate insert
                if($insert_row){
                    continue;
                } else {
                    die('Error : ('.$mysqli->errno . ') '. $mysqli->error);
                }
            }
        }
        $msg = 'Question has been added';
    }
}

/*
* Get total questions
*/
$query = "SELECT * FROM `questions` WHERE status='1'";
//Get The Results
$questions = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total = $questions->num_rows;
$next = $total+1;
?>
        <form method="post" action="add.php">
            <p>
                <label>Question Number: </label>
                <input type="number" value="<?php echo $next; ?>" name="question_no" />
            </p>
            <p>
                <label>Question Text: </label>
                <input type="text" name="question_text" required/>
            </p>
            <p>
                <label>Choice #1: </label>
                <input type="text" name="choice1" required/>
            </p>
            <p>
                <label>Choice #2: </label>
                <input type="text" name="choice2" required/>
            </p>
            <p>
                <label>Choice #3: </label>
                <input type="text" name="choice3" required/>
            </p>
            <p>
                <label>Choice #4: </label>
                <input type="text" name="choice4" required/>
            </p>
            <p>
                <label>Choice #5: </label>
                <input type="text" name="choice5" required/>
            </p>
            <p>
                <label>Correct Choice Number: </label>
                <input type="number" name="correct_choice" />
            </p>
            <p>
                <input type="submit" name="submit" value="Submit" />
            </p>
        </form>

3 个答案:

答案 0 :(得分:1)

由于您的question_number是插入问题中的自动递增ID,因此您需要将其插入question_number中新行的choices列中。

通过

获取最后插入的ID
$insert_row->insert_id;

我已经修改了您的代码以简化代码,并使用准备好的语句将其转换为您准备插入questions的语句。

按原样在循环中插入多行时,使用预处理语句还有其他好处。您准备查询一次,并执行多次。

if ($insert_row) {
    $question_number = $insert_row->insert_id;

    $query = "INSERT INTO `choices` (question_number, is_correct, text) VALUES (?, ?, ?)";
    $stmt = $mysqli->prepare($query) or die($mysqli->error.__LINE__);
    foreach ($choices as $choice => $value) {
        if (!empty($value) {
            $is_correct = (int)($correct_choice == $choice);
            //Choice query

            //Run query
            $stmt->bind_param('sss', $question_number, $is_correct, $value);
            $stmt->execute();

            if ($stmt->errno) {
                die('Error : ('.$stmt->errno . ') '. $stmt->error);
            }
        }
    }
    $stmt->close();
    $msg = 'Question has been added';
}

答案 1 :(得分:1)

在您选择的查询中,您插入了$question_number,但它应该是您在问题表中插入的最后一个插入ID,请使用此$last_id = mysqli_insert_id($conn);

//Updated Choice query
  $query = "INSERT INTO `choices` (question_number, is_correct, text)
                    VALUES ('$last_id','$is_correct','$value')";

希望这会有所帮助

答案 2 :(得分:0)

$last_id = $mysqli->insert_id; 

使用此查询