我的PHP代码运行下一个呈现的html页面,而不是当前的

时间:2014-04-08 09:21:13

标签: php html mysql

我需要为学校制作一个项目,所以我决定制作一个测验游戏网页。

每个问题都有4个答案。

每个问题,其'4个答案及其'正确答案都是从数据库中随机获取的。

问题是我无法弄清楚如何修复此错误: 例如,显示的第一个问题有B)正确的答案。 第二个问题有C)正确答案。

如果我选择B作为我的答案,在问题一中,它不会像预期的那样显示“正确答案”,但是“答案不正确”。

如果我选择C作为我的答案,在第一个问题上,它不会像预期的那样显示“错误答案”,而是“正确答案”。

这是我的代码:`             

        $connection=mysql_connect('localhost','root',"");
        if(!$connection)
        {
            die("error".mysql_error());
        }

        $db_select=mysql_select_db("joculmintii",$connection);

        if(!$db_select)
        {
            die("error".mysql_error());
        }

            echo "<form id='formintrebare' method='post'>";

            $sub_result=mysql_query("SELECT * from intrebari ORDER BY rand() LIMIT 1",$connection);

            if(!$sub_result)
            {
                die("database query failed". mysql_error());
            }

            while ($sub_row=mysql_fetch_array($sub_result))
            {

                $question=$sub_row["intrebare"];
                $option1=$sub_row["raspuns1"];
                $option2=$sub_row["raspuns2"];
                $option3=$sub_row["raspuns3"];
                $option4=$sub_row["raspuns4"];
                $answer=$sub_row["raspunscorect"];

                echo "<div id='intrebari'>
                    `<h3>Q:".$question."</h3>";   
                echo"
                    <input type= submit  name=\"raspuns1\" value=\"{$option1}\" class=\"imgClass\">
                    <input type= submit  name=\"raspuns2\" value=\"{$option2}\" class=\"imgClass\">
                    </br>
                    <input type= submit  name=\"raspuns3\" value=\"{$option3}\" class=\"imgClass\">
                    <input type= submit  name=\"raspuns4\" value=\"{$option4}\" class=\"imgClass\">
                    </br>
                    </br>

                    </div>";


                echo"</form>";

            }

                if(isset($_POST['raspuns1']))   
                {

                        if($answer==="raspuns1")
                        {
                            echo '<script language="javascript">';
                            echo 'alert("Raspuns corect");';
                            echo '</script>';
                        }
                        else
                        {
                            echo '<script language="javascript">';
                            echo 'alert("Raspuns gresit");';
                            echo '</script>';
                        }   
                } 

                elseif(isset($_POST['raspuns2']))   
                {
                        if($answer==="raspuns2")
                        {
                            echo '<script language="javascript">';

                            echo 'alert("Raspuns corect");';

                            echo '</script>';
                        }   
                        else
                        {
                            echo '<script language="javascript">';
                            echo 'alert("Raspuns gresit");';
                            echo '</script>';
                        }   
                } 

                elseif (isset($_POST["raspuns3"]))   
                {
                        if($answer==="raspuns3")
                        {
                            echo '<script language="javascript">';
                            echo 'alert("Raspuns corect");';
                            echo '</script>';
                        }   
                        else
                        {
                            echo '<script language="javascript">';
                            echo 'alert("Raspuns gresit");';
                            echo '</script>';
                        }   
                }

                elseif (isset($_POST['raspuns4'])) 
                {
                        if($answer==="raspuns4")
                        {
                            echo '<script language="javascript">';
                            echo 'alert("Raspuns corect");';
                            echo '</script>';
                        }
                        else
                        {
                            echo '<script language="javascript">';
                            echo 'alert("Raspuns gresit");';
                            echo '</script>';
                        }   
                }




     ?>`

非常感谢任何帮助。

PS:英语不是我的母语,对不起。

2 个答案:

答案 0 :(得分:1)

你用错误的问题检查答案。

每当您执行POST并更新页面时,您都会将当前答案作为POST变量$_POST['raspunsN']发送。然后,您随机从数据库中获取一个新问题,然后检查旧问题的答案以及新问题的答案。

你必须以某种方式存储你的旧回答并检查。一种简单的方法是使用PHP Sessions。

这是一个可能看起来如何的例子

            session_start();
            if(isset($_POST['raspuns1']))   
            {
                $answer = $_SESSION['answer'];
                    if($answer==="raspuns1")
                    {
                        echo '<script language="javascript">';
                        echo 'alert("Raspuns corect");';
                        echo '</script>';
                    }
                    else
                    {
                        echo '<script language="javascript">';
                        echo 'alert("Raspuns gresit");';
                        echo '</script>';
                    }   
            } 

            elseif(isset($_POST['raspuns2']))   
            {
                    if($answer==="raspuns2")
                    {
                        echo '<script language="javascript">';

                        echo 'alert("Raspuns corect");';

                        echo '</script>';
                    }   
                    else
                    {
                        echo '<script language="javascript">';
                        echo 'alert("Raspuns gresit");';
                        echo '</script>';
                    }   
            } 

            elseif (isset($_POST["raspuns3"]))   
            {
                    if($answer==="raspuns3")
                    {
                        echo '<script language="javascript">';
                        echo 'alert("Raspuns corect");';
                        echo '</script>';
                    }   
                    else
                    {
                        echo '<script language="javascript">';
                        echo 'alert("Raspuns gresit");';
                        echo '</script>';
                    }   
            }

            elseif (isset($_POST['raspuns4'])) 
            {
                    if($answer==="raspuns4")
                    {
                        echo '<script language="javascript">';
                        echo 'alert("Raspuns corect");';
                        echo '</script>';
                    }
                    else
                    {
                        echo '<script language="javascript">';
                        echo 'alert("Raspuns gresit");';
                        echo '</script>';
                    }   
            }  

    $connection=mysql_connect('localhost','root',"");
    if(!$connection)
    {
        die("error".mysql_error());
    }

    $db_select=mysql_select_db("joculmintii",$connection);

    if(!$db_select)
    {
        die("error".mysql_error());
    }

        echo "<form id='formintrebare' method='post'>";

        $sub_result=mysql_query("SELECT * from intrebari ORDER BY rand() LIMIT 1",$connection);

        if(!$sub_result)
        {
            die("database query failed". mysql_error());
        }

        while ($sub_row=mysql_fetch_array($sub_result))
        {

            $question=$sub_row["intrebare"];
            $option1=$sub_row["raspuns1"];
            $option2=$sub_row["raspuns2"];
            $option3=$sub_row["raspuns3"];
            $option4=$sub_row["raspuns4"];
            $_SESSION['answer'] = $sub_row["raspunscorect"];

            echo "<div id='intrebari'>
                `<h3>Q:".$question."</h3>";   
            echo"
                <input type= submit  name=\"raspuns1\" value=\"{$option1}\" class=\"imgClass\">
                <input type= submit  name=\"raspuns2\" value=\"{$option2}\" class=\"imgClass\">
                </br>
                <input type= submit  name=\"raspuns3\" value=\"{$option3}\" class=\"imgClass\">
                <input type= submit  name=\"raspuns4\" value=\"{$option4}\" class=\"imgClass\">
                </br>
                </br>

                </div>";


            echo"</form>";

        }

请考虑使用重复的代码制作一个功能:

    if($answer==="raspuns1")
    {
        echo '<script language="javascript">';
        echo 'alert("Raspuns corect");';
        echo '</script>';
    }
    else
    {
        echo '<script language="javascript">';
        echo 'alert("Raspuns gresit");';
        echo '</script>';
    }    

祝你在学习编码和PHP的过程中好运!

答案 1 :(得分:0)

请使用这样的东西。它会起作用:

if(isset($_POST['raspuns1']))   
                {

                        if($_POST['raspuns1']==$answer)
                        {
                            echo '<script language="javascript">';
                            echo 'alert("Raspuns corect");';
                            echo '</script>';
                        }
                        else
                        {
                            echo '<script language="javascript">';
                            echo 'alert("Raspuns gresit");';
                            echo '</script>';
                        }   
                }