0

我正在创建一个多项选择测验,但我不知道如何将多个数组插入数据库。我可以使用 array_combine 插入前 2 个数组,但不能插入第 3、第 4、第 5 和第 6 个数组。我可以插入问题和答案,但不能插入选项 A、B、C 和 D。

我正在尝试做类似的事情

foreach(array_combine($_POST['inQuestion'], $_POST['inAnswer'], $_POST['inA'], $_POST['inB'], $_POST['inC'], $_POST['inD' ]) 作为 $question => $answer => $A => $B => $C => $D) {

请帮忙。这是我的代码。

<?php
    if(isset($_POST['btnCreate'])) {

        $inQuestion = array($_POST['inQuestion']);
        $inAnswer = array($_POST['inAnswer']);
        $inA = array($_POST['inA']);
        $inB = array($_POST['inB']);
        $inC = array($_POST['inC']);
        $inD = array($_POST['inD']);
        $inLesson = $_POST['inLesson'];
        $inQuizNo = $_POST['inQuizNo'];

        $sql = "SELECT * FROM lessons WHERE title='$inLesson'";
        $query = mysql_query ($sql);
        $row = mysql_fetch_assoc($query);

        $lessonID = $row['lessonID'];

        foreach(array_combine($_POST['inQuestion'], $_POST['inAnswer']) as $question => $answer) {

            $sql = "INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES ('$question', '$answer', '$A', '$B', '$C', '$D', '$lessonID', $inQuizNo)";
            $query = mysql_query( $sql ); 
        }

    }
?>

<div class="panel panel-info">
<div class="panel-heading">
    <h4>Create Assessment - Multiple Choice</h4>
</div>

<div class="panel-body">

    <form method="post">

        <br>

        <div>
            <div class="col-md-4">
                <strong>Lesson</strong><br>
                <select class="form-control" name="inLesson">
                    <option></option>
                    <option disabled></option>

                    <?php
                        $sql2 = "SELECT * FROM lessons WHERE courseID='$_GET[courseID]'";
                        $query2 = mysql_query ($sql2);                          
                        while ($row2 = mysql_fetch_assoc($query2)) {
                    ?>

                        <option><?php echo $row2['title']; ?></option>

                    <?php   } ?>

                </select>
            </div>
            <div class="col-md-4">
                <strong>Quiz No.</strong><br>                   
                <input required type="number" class="form-control" name="inQuizNo" min="1" max="100">
            </div>
            <div class="col-md-4">
                <div class="pull-right">
                <br>
                <input type="button" value="Add" id="addButton" class="btn btn-info">
                <input type="button" value="Remove" id="removeButton" class="btn btn-warning">
                </div>
            </div>
        </div>

        <br><br><br><br><br>

        <div id='TextBoxesGroup'>
            <div id="TextBoxDiv1" class="form-group">

                <h2><span class="label label-primary">No. 1</span></h2>
                <br><br>

                <div class="col-md-10">
                    <label>Question</label><input type='text' id='textbox1' name='inQuestion[]' class="form-control">
                </div>
                <div class="col-md-2">
                    <label>Answer</label><input type='text' id='textbox1' name='inAnswer[]' class="form-control">
                    <br>
                </div>

                <div class="col-md-3">
                    <label>A</label><input type='text' id='textbox1' name='inA[]' class="form-control">
                </div>
                <div class="col-md-3">
                    <label>B</label><input type='text' id='textbox1' name='inB[]' class="form-control">
                </div>
                <div class="col-md-3">
                    <label>C</label><input type='text' id='textbox1' name='inC[]' class="form-control">
                </div>
                <div class="col-md-3">
                    <label>D</label><input type='text' id='textbox1' name='inD[]' class="form-control">
                    <br><br>
                </div>
            </div>
        </div>

        <div class="col-md-12">
            <button type="submit" name="btnCreate"  class="btn btn-success pull-right">Create Quiz
        </div>
    </form>
</div>

4

3 回答 3

1

据我了解,您正在尝试将多行插入到您的数据库中。

首先,当您使用[]HTML 表单时,您不必将其声明为php

二、每道题都会有题名,答案,A,B,C,D

当您尝试创建 HTML 表单时,您可以使用此格式来跟踪 question_id 或编号。

<label>Question</label><input type='text' id='textbox1' name='inQuestion[1]' class="form-control">

然后您将能够检索此项目$_POST['inQuestion'][1]

例如,如果您的测试页面需要 10 个问题,您可以这样做:

<?php for($i = 0; $i <= 10; $i++) : ?>
<form method="post"> 

   <div class="col-md-10">
        <label>Question</label><input type='text' id='textbox1' name='inQuestion[<?=$i;?>]' class="form-control">
    </div>

    <div class="col-md-2">
        <label>Answer</label><input type='text' id='textbox1' name='inAnswer[<?=$i;?>]' class="form-control">
        <br>
    </div>


    <div class="col-md-3">
        <label>A</label><input type='text' id='textbox1' name='inA[<?=$i;?>]' class="form-control">
    </div>
    <div class="col-md-3">
        <label>B</label><input type='text' id='textbox1' name='inB[<?=$i;?>]' class="form-control">
    </div>
    <div class="col-md-3">
        <label>C</label><input type='text' id='textbox1' name='inC[<?=$i;?>]' class="form-control">
    </div>
    <div class="col-md-3">
        <label>D</label><input type='text' id='textbox1' name='inD[<?=$i;?>]' class="form-control">
        <br><br>
    </div>


    <input type="submit" value="go">

</form>

<?php endfor; ?>

另外,为什么将答案 A、B、C、D 的内容放入一个数组中,而将它们放在数据库中的列中。也在您的查询中使用这些变量。

$inA = $_POST['inA'];
$inB = $_POST['inB'];
$inC = $_POST['inC'];
$inD = $_POST['inD'];

最后让我们现在尝试插入您的值。

if($_POST['btnCreate']){

    // The form has been posted!

    $inLesson = $_POST['inLesson'];
    $inQuizNo = $_POST['inQuizNo'];

    $questionsCount = count($_POST['inQuestion']);

    $items = array();

    // Since we should have the same size of arrays.
    // Also the empty validation has been handled before this step!
    for($i = 0; $i < $questionsCount; $i++){
            $temp = array();
        $temp['inQuestion']   = $_POST['inQuestion'][$i];
        $temp['inAnswer']     = $_POST['inAnswer'][$i];
        $temp['inA']          = $_POST['inA'][$i];
        $temp['inB']          = $_POST['inB'][$i];
        $temp['inC']          = $_POST['inC'][$i];
        $temp['inD']          = $_POST['inD'][$i];
            $items[] = $temp;
    }


    // Now items should have an array of all questions.

    foreach($items as $item){

        $SQL = 'INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES (
                                                                                            "'.$item['inQuestion'].'",
                                                                                            "'.$item['inAnswer'].'",
                                                                                            "'.$item['inA'].'",
                                                                                            "'.$item['inB'].'",
                                                                                            "'.$item['inC'].'",
                                                                                            "'.$item['inD'].'",
                                                                                            "'.$inLesson.'",
                                                                                            "'.$inQuizNo.'")';

       $query = mysql_query( $SQL );
    } // End Foreach
}
于 2013-09-28T09:55:41.903 回答
0

尝试这个

 <?php
    if(isset($_POST['btnCreate'])) {

        $inQuestion = array($_POST['inQuestion']);
        $inAnswer = array($_POST['inAnswer']);
        $inA = $_POST['inA'] ? $_POST['inA'] : null;
        $inB = $_POST['inB'] ? $_POST['inB'] : null;
        $inC = $_POST['inC'] ? $_POST['inC'] : null;
        $inD = $_POST['inD'] ? $_POST['inD'] : null;
        $inLesson = $_POST['inLesson'];
        $inQuizNo = $_POST['inQuizNo'];

        $sql = "SELECT * FROM lessons WHERE title='$inLesson'";
        $query = mysql_query ($sql);
        $row = mysql_fetch_assoc($query);

        $lessonID = $row['lessonID'];

            $sql = "INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES ('$question', '$answer', '$inA', '$inB', '$inC', '$inD', '$lessonID', $inQuizNo)";
            $query = mysql_query( $sql ); 
    }
?>

<div class="panel panel-info">
<div class="panel-heading">
    <h4>Create Assessment - Multiple Choice</h4>
</div>

<div class="panel-body">

    <form method="post">

        <br>

        <div>
            <div class="col-md-4">
                <strong>Lesson</strong><br>
                <select class="form-control" name="inLesson">
                    <option></option>
                    <option disabled></option>

                    <?php
                        $sql2 = "SELECT * FROM lessons WHERE courseID='$_GET[courseID]'";
                        $query2 = mysql_query ($sql2);                          
                        while ($row2 = mysql_fetch_assoc($query2)) {
                    ?>

                        <option><?php echo $row2['title']; ?></option>

                    <?php   } ?>

                </select>
            </div>
            <div class="col-md-4">
                <strong>Quiz No.</strong><br>                   
                <input required type="number" class="form-control" name="inQuizNo" min="1" max="100">
            </div>
            <div class="col-md-4">
                <div class="pull-right">
                <br>
                <input type="button" value="Add" id="addButton" class="btn btn-info">
                <input type="button" value="Remove" id="removeButton" class="btn btn-warning">
                </div>
            </div>
        </div>

        <br><br><br><br><br>

        <div id='TextBoxesGroup'>
            <div id="TextBoxDiv1" class="form-group">

                <h2><span class="label label-primary">No. 1</span></h2>
                <br><br>

                <div class="col-md-10">
                    <label>Question</label><input type='text' id='textbox1' name='inQuestion[]' class="form-control">
                </div>
                <div class="col-md-2">
                    <label>Answer</label><input type='text' id='textbox1' name='inAnswer[]' class="form-control">
                    <br>
                </div>

                <div class="col-md-3">
                    <label>A</label><input type='text' id='textbox1' name='inA' class="form-control">
                </div>
                <div class="col-md-3">
                    <label>B</label><input type='text' id='textbox1' name='inB' class="form-control">
                </div>
                <div class="col-md-3">
                    <label>C</label><input type='text' id='textbox1' name='inC' class="form-control">
                </div>
                <div class="col-md-3">
                    <label>D</label><input type='text' id='textbox1' name='inD' class="form-control">
                    <br><br>
                </div>
            </div>
        </div>

        <div class="col-md-12">
            <button type="submit" name="btnCreate"  class="btn btn-success pull-right">Create Quiz
        </div>
    </form>
</div>
于 2013-09-28T08:59:39.710 回答
0

与其发布详细的解决方案,不如尝试如何插入多行以获得一些想法

就像是:

$_POST['inQuestion'] = array(
  'q1','q2','q3' // from input array name 'inQuestion[]'
);

$_POST['inAnswer'] = array(
  'a1','a2','a3' // from input array name 'inAnswer[]'
);

$_POST['other'] = 'sunshine';

$_POST['yet_another'] = 'nice weather';

foreach($_POST['inQuestion'] as $k => $v){
$inserts[] = "(".$v.",".$_POST['inAnswer'][$k].",".$_POST['other'].",".$_POST['yet_another'].")";
}

echo "INSERT INTO table col_1, col_2, col_3, col_4 VALUES ".implode(',',$inserts)."";

// INSERT INTO table col_1, col_2, col_3, col_4 VALUES (q1,a1,sunshine,nice weather),(q2,a2,sunshine,nice weather),(q3,a3,sunshine,nice weather)
于 2013-09-28T10:16:57.047 回答