0

MySQLから一連のラジオボタンを動的に生成しています。エコーprint_rを実行したときと同じように、ボタンが作成され、ボタンに割り当てられた変数が入力され、変数の配列が表示されます。ここで、これから生成された値を比較したいと思います。値が「0」の場合、スコアを挿入し、緑色のチェックグラフィックと正しい単語を表示します。値が「1」の場合、スコアに異なる値を入力して、不正解と赤いXグラフィックを表示します。これが私がこれまでに持っているものです(すべてがラジオボタンとして質問と回答の両方に動的に入力されます):

<?php
echo '<form id="frmQuestion" name="frmQuestion" method="post" action="QuizQuestion1.php">';

// Connect to the Database
require_once('mysqli_connect.php');

//create the query for the question
$q = "SELECT `Question` FROM tbl_Question WHERE QuestionID = 1";

//Create the query for the Answers
$q2 = "SELECT `Answer`,`AnswerStatusID`,`AnswerResponse` FROM tbl_Answer WHERE QuestionID = 1";

//Run the query
$r = mysqli_query($conn,$q);

//run the answer query
$r2 = mysqli_query($conn,$q2);

while($row = mysqli_fetch_array($r,MYSQLI_ASSOC)){
    echo '<div id="Question1"><p> ' . $row['Question'] . '</div></p>';
}

//Declare the variables as a array
$AnswerResponse = array();
$AnswerStatusID = array();

while($row2 = mysqli_fetch_array($r2,MYSQLI_ASSOC)){
    echo '<div id="Question1"><input name="q1" type="radio" value="'.$AnswerStatusID.'"/>' . $row2['Answer'] . '</div><br/>';

    //Assign the AnswerStatusID to a var
    $AnswerStatusID[] = $row2['AnswerStatusID'];

    //Assign the AnswerResponse to a var
    $AnswerResponse[] = $row2['AnswerResponse'];
}

//Create the submit button 
echo '<input type="submit" value="Submit Answer" name="submit"/>';
echo '</form>';

//Logic for correct or incorrect answers
if (isset($_POST['q1']) && ($_POST['q1'] == '0'))
{
    //create the query for the score
    $q3 = "INSERT INTO tbl_Score (`Score`,`QuestionID`) VALUES ('100%','1')";   

    //Run the query 
    $r = @mysqli_query ($conn,$q3);

    if($r){

        //Confirm message data was entered with a correct response and a graphic
        echo '<h1>Correct!!</h1><img src="/images/green_Check_Low.jpg" alt="Green Check"/>';
        echo '<a href="QuizQuestion2.php">Click here for the next question</a>';
    }
    else
    {
        //there was an error
        echo'<h1>System error</h1>';

        //Debugging message
        echo'<p>' . mysqli_error($conn) . '<br/><br/>Query:' . $q3 . '</p>';

    }//End of nested IF
}
else{
    //create the query for the score
    $q4 = "INSERT INTO tbl_Score (`Score`,`QuestionID`) VALUES ('0%','1')"; 

    //Run the query 
    $r2 = @mysqli_query ($conn,$q3);

    if($r2){

        //Confirm message data was entered with a correct response and a graphic
        echo '<h1>Incorrect!!</h1><img src="/images/red_X_Low.jpg" alt="Red X"/>';
        echo '<a href="QuizQuestion2.php">Click here for the next question</a>';
    }
    else
    {
        //there was an error
        echo'<h1>System error</h1>';

        //Debugging message
        echo'<p>' . mysqli_error($conn) . '<br/><br/>Query:' . $q3 . '</p>';

    }//End of nested IF

}

//Free up the results for the Question query
mysqli_free_result($r);

//Free up the results from the Answer query
mysqli_free_result($r2);

//close the DB connection
mysqli_close($conn); 

?>
4

1 に答える 1

0

これが答えであり、意図したとおりに機能します。皆様のご意見をお寄せいただきありがとうございます。

//Declare the variables as a array
$AnswerResponse = array();
$AnswerStatusID = array();

while($row2 = mysqli_fetch_array($r2,MYSQLI_ASSOC)){
echo '<div id="Question1"><input name="q1" type="radio" value="'.$row2['AnswerStatusID'].'"/>' . $row2['Answer'] . '</div><br/>';

//Assign the AnswerStatusID to a var
$AnswerStatusID[] = $row2['AnswerStatusID'];

//Assign the AnswerResponse to a var
$AnswerResponse[] = $row2['AnswerResponse'];
}




//Create the submit button 
 echo '<input type="submit" value="Submit Answer" name="submit"/>';
echo '<input type="hidden"name="submitted"value="TRUE"/>';
echo '</form>';



if($_POST['submitted']) { 

//Logic for correct or incorrect answers
if (isset($_POST['q1']) && ($_POST['q1'] == '0'))
{
//create the query for the score
$q3 = "INSERT INTO tbl_Score (`Score`,`QuestionID`) VALUES ('100%','1')";   

//Run the query 
$r3 = @mysqli_query ($conn,$q3);

//Confirm message data was entered with a correct response and a graphic
echo '<h1>Correct!!</h1><img src="/images/green_Check_Low.jpg" alt="Green Check"/>';
echo '<a href="QuizQuestion2.php">Click here for the next question</a>';


}       
else{
//create the query for the score
$q4 = "INSERT INTO tbl_Score (`Score`,`QuestionID`) VALUES ('0%','1')"; 

//Run the query 
$r4 = @mysqli_query ($conn,$q4);

//Confirm message data was entered with a correct response and a graphic
echo '<h1>Incorrect!!</h1><img src="/images/red_X_Low.jpg" alt="Red X"/><br/>';
echo '<a href="QuizQuestion1.php">Click here to try again</a>';

}

}
于 2012-07-12T18:18:30.263 に答える