0

長い投稿で申し訳ありません。quiz1.php で動作するように index.html を取得しましたが、質問とチェックボックスを正しく動作させることができません。つまり、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) 2 ページ目 (先頭に 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) 3 ページ目 (1 ページ目と 2 ページ目の情報をデータベースに入力)

<?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

Try this in your quiz1.php.

Effectively your </form> was outside the loop, while <form...> was inside the loop. This was not constructing proper HTML and most likely was the reason why this would not work.

Also your third script was expecting the question element in the $_POST array which was never set or sent. Using a hidden field rectifies that.

$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>';

Finally PLEASE PLEASE PLEASE try to change your code to use PDO or mysqli. mysql* functions have been deprecated and should not be used. There are plenty of tutorials and resources in this site that will help you with this.

于 2012-11-05T23:27:52.007 に答える