0

对不起,很长的帖子。我让 index.html 与 quiz1.php 一起工作,但我无法让问题和复选框正常工作,这意味着将 index.html 中的其他信息插入到数据库中。它应该插入来自 index.html 的信息和来自 quiz1.php 的信息。需要帮助才能使其正常工作。

这是我到目前为止所做的。

表单 (index.html) 第一页(获取人员信息)

<form action="quiz1.php" method="post">
    Full Name: <input type="text" name="full_name" />
    Quiz Name: <input type="text" name="quiz_name" />
    Class Name: <input type="text" name="class_name" />
    Date: <input type="text" name="quiz_taken" />
    <input type="submit" value="Submit" class="flashit">
</form>

测验 (quiz1.php) 第二页(将 index.html 中的信息放在顶部)

<?php
// info from index.html
$_SESSION['full_name']  = $_POST['full_name'];
$_SESSION['quiz_name']  = $_POST['quiz_name'];
$_SESSION['class_name'] = $_POST['class_name'];
$_SESSION['quiz_taken'] = $_POST['quiz_taken'];

echo $_SESSION['full_name'];
echo "<br />";
echo $_SESSION['quiz_name'];
echo "<br />";
echo $_SESSION['class_name'];
echo "<br />";
echo $_SESSION['quiz_taken'];
echo "<br />";
echo "<br />";

?>

// quiz info

<?php

$db_host = "localhost"; 
// Place the username for the MySQL database here 
$db_username = "root";  
// Place the password for the MySQL database here 
$db_pass = "";  
// Place the name for the MySQL database here 
$db_name = "test"; 

// Run the actual connection here  
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");

//retreive questions from database and put into question box
$query2 = "SELECT `id`, `question`, `aee`, `bee`, "
        . "`cee`, `dee`, `quizAnswer` FROM `quiz1question`";

$question2    = mysql_query($query2);
$answerFields = array(
                    'aee'=>'aee', 
                    'bee'=>'bee', 
                    'cee'=>'cee', 
                    'dee'=>'dee'
                );

while ($row = mysql_fetch_array($question2))
{

    $id         = $row['id'];
    $question   = $row['question'];

    echo '<form action="insert.php" method="post">';

    // Print Question
    printf('<div id="ContainerQuestion">');
    printf('<span class="Question">%s. %s</span>', $id, $question);

    // Print Answers
    foreach ($answerFields as $field=>$ans)
    {
        if (array_key_exists($field, $row) && $row[$field])
        {
            $checked = ($row["quizAnswer"] == $ans) ? 'checked' : '';
            printf(
                '<p><input type="checkbox" name="%s" %s value="%s">%s</p>', 
                $id, 
                $checked, 
                $ans, 
                $row[$field]
            );
        }
    }
}

 echo '<input type="submit" value="Submit Quiz"></form>';

?>

results (insert.php) 第三页(将第一页和第二页的信息输入数据库)

<?php 

$localhost = "localhost";
$username  = "root";
$password  = "";
$database  = "test";
$table     = "quiz_results";

mysql_connect("$localhost","$username","$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());

// question & answers
$mysql1 = "INSERT INTO $table (question, aee, bee, cee, dee) "
        . "VALUES ('$_POST[question]','$_POST[aee]',"
        . "'$_POST[bee]','$_POST[cee]','$_POST[dee]')";

if(!mysql_query($mysql1)) 
{
    die(mysql_error());
}

// insert Name, quiz name, class name, and quiz taken 
$mysql = "INSERT INTO $table (full_name, quiz_name, class_name, quiz_taken) "
       . "VALUES ('$_POST[full_name]','$_POST[quiz_name]',"
       . "'$_POST[class_name]','$_POST[quiz_taken]')";

if(!mysql_query($mysql))
{
    die(mysql_error());
}

// echo
echo"Thank you!"; // mysql1
echo "<br />";
echo"Your Quiz has been Inserted"; // mysql

mysql_close();

?>
4

1 回答 1

0

在你的 quiz1.php 中试试这个。

实际上,您</form>在循环之外,而<form...>在循环内。这没有构建正确的 HTML,很可能是这不起作用的原因。

此外,您的第三个脚本期待数组中从未设置或发送的question元素。$_POST使用隐藏字段可以纠正这一点。

$answerFields = array(
                    'aee' => 'aee', 
                    'bee' => 'bee', 
                    'cee' => 'cee', 
                    'dee' => 'dee'
                );

echo "<form action='insert.php' method='post'>";

while ($row = mysql_fetch_array($question2))
{

    $id         = $row['id'];
    $question   = $row['question'];


    echo "<input type='hidden' name='question' value='{$question}' />"

    // Print Question
    printf('<div id="ContainerQuestion">');
    printf('<span class="Question">%s. %s</span>', $id, $question);

    // Print Answers
    foreach ($answerFields as $field=>$ans)
    {
        if (array_key_exists($field, $row) && $row[$field])
        {
            $checked = ($row["quizAnswer"] == $ans) ? 'checked' : '';
            printf(
                '<p><input type="checkbox" name="%s" %s value="%s">%s</p>', 
                $id, 
                $checked, 
                $ans, 
                $row[$field]
            );
        }
    }

}

echo '<input type="submit" value="Submit Quiz">';
echo '</form>';

最后尝试更改您的代码以使用PDOmysqli。mysql* 函数已被弃用,不应使用。这个网站上有很多教程和资源可以帮助你。

于 2012-11-05T23:27:52.007 回答