php表单提交que。和ans。在线考试

时间:2012-07-11 07:43:22

标签: php arrays forms

插入查询的时间过长。

我的表单有1个问题,4个选项在while循环中使用单选按钮。从名为paper的表中提取问题和选项,我使用数组来提问和回答。以下是我使用的代码。

<form id="form1" action="submit_answer.php" method="post">
<?php
$get_question = mysql_query("select * from paper where test_name='$test_name' ORDER BY RAND()");
$count = 0;
$sr = 1;
if(mysql_num_rows($get_question)==0)
{
    echo "No Questions Found For ".$test_name;
}
else
{
    while($row_question = mysql_fetch_array($get_question))
    {
        $id = $row_question["id"];
        $question = $row_question["question"];
        $option1 = $row_question["option1"];
        $option2 = $row_question["option2"];
        $option3 = $row_question["option3"];
        $option4 = $row_question["option4"];
        $answer = $row_question["answer"];

        echo '<div class="question_box">
                <p>'.$sr.'. '.$question.'</p>
                <div style="margin:10px 0 0 10px;">

                    <input type="text" value="'.$std.'" name="std" />
                    <input type="text" value="'.$student_name.'" name="student_name" />
                    <input type="text" value="'.$test_name.'" name="test_name" />
                    <input type="text" value="'.$question.'" name="question[]" />
                    <input type="hidden" value="'.$answer.'" name="true_answer[]" />
                    <input type="radio" class="answers" name="given_answer['.$count.']" value="A" />&nbsp;'.$option1.'<br/>
                    <input type="radio" class="answers" name="given_answer['.$count.']" value="B" />&nbsp;'.$option2.'<br/>
                    <input type="radio" class="answers" name="given_answer['.$count.']" value="C" />&nbsp;'.$option3.'<br/>
                    <input type="radio" class="answers" name="given_answer['.$count.']" value="D" />&nbsp;'.$option4.'<br/>
                    <input checked="checked" type="radio" class="answers" name="given_answer['.$count.']" value="NONE" style="display:none;" />
                </div>
        </div>';
        $count++;
        $sr++;
    }
}
?>
    <button class="stdbtn btn_black" style="opacity: 1;" type="button" onclick="submit_exam();">Submit</button>
</form>

现在,submit_answer.php页面用于提交所有问题和答案,包括学生标准,学生姓名,考试名称,问题和答案。以下是我使用的代码:

foreach($_POST as $key => $value)
{
    echo $key;
    echo "<br />";
    print_r($value);
    echo "<br />";
    echo "<br />";

    mysql_query("insert into student_answer($key) values($value)");

    echo mysql_error();
}

它确实插入了学生的标准而不是其他值,并且给了我错误。

**Unknown column 'Array' in 'field list'**

表格字段为:

+------++--------------++-----------++----------++--------------+
| std  || student_name || test_name || question || given_answer |
+------++--------------++-----------++----------++--------------+

请在上面的查询中帮助我。提前致谢!! :)

3 个答案:

答案 0 :(得分:0)

您已使用$given_answer作为数组参数,因此它会在array中返回$key。尝试使用隐藏的参数计数,然后插入$key[$count]

答案 1 :(得分:0)

LOL我得到了自己的答案,查询将如下所示:

$count = $_POST["count"];    // hidden value under the while loop

for($i=1; $i<=$count; $i++)
{
   $test_name = $_POST["test_name"];
   $std = $_POST["std"];
   $student_name = $_POST["student_name"];
   $question = $_POST["question"][$i-1];
   $given_answer = $_POST["given_answer"][$i-1];

   mysql_query("insert into student_answer(std,student_name,test_name,question,given_answer) values('$std','$student_name','$test_name','$question','$given_answer')");

    echo mysql_error();

}

答案 2 :(得分:0)

我更喜欢你在市场上购买可用的在线脚本作为它的起点。这将节省您的时间,成本和测试工作。

下面是我使用它的一个很好的脚本,它像魅力一样工作。以此为基础,我使用计算机自适应测试开发了一个超过1000个用户的在线测试门户。

http://codecanyon.net/item/online-skills-assessment/9379895

对于希望开发在线考试系统的人来说,这是一个很好的起点