0

我正在从 MySQL 动态生成一组单选按钮。按钮正在创建,分配给它们的变量正在填充,就像我执行 echo 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 回答