开发者

Making quiz game in php with mysql

<?php
        $connection=mysql_connect('localhost','root',"");
        if(!$connection)
        {
            die("database connection failed".mysql_error());
        }
        $db_select=mysql_select_db("quiz_game",$connection);
        if(!$db_select)
        {
            die("database connection failed".mysql_error());
        }
        if(!isset($_POST['submit']))
        {
            echo "<form method=\"post\" action=\"air.php\">";
            $sub_result=mysql_query("SELECT * from questions where category='air' order by rand() limit 0,5",$connection);
            if(!$sub_result)
            {
                die("database query failed". mysql_error());
            }

            while ($sub_row=mysql_fetch_array($sub_result))
            {
                $id=$sub_row["qno"];
                $question=$sub_row["question"];
                $option1=$sub_row["option1"];
                $option2=$sub_row["option2"];
                $option3=$sub_row["option3"];
                $option4=$sub_row["option4"];
                $answer=$sub_row["answer"];

                echo "<h3>Q".$id." :".$question."</br></h3>";   
                echo"</br>
                    $option1
                    <input type= radio name=\"{$id}\" value=\"{$option1}\" >
          开发者_开发知识库          </br>
                    $option2
                    <input type= radio name=\"{$id}\" value=\"{$option2}\">
                    </br>
                    $option3
                    <input type= radio name=\"{$id}\" value=\"{$option3}\">
                    </br>
                    $option4
                    <input type= radio name=\"{$id}\" value=\"{$option4}\">
                    </br></br>";
            }
            echo"<input type='submit' value='see how you did it' name='submit'>";
            echo"</form>";

        }


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

            $total=0;
            $answer=mysql_query("select qno,answer from questions",$connection);
            while($ans=mysql_fetch_assoc($answer))
            {
                if($_POST[$ans['qno']]==$ans['answer'])
                {
                    $total++;
                }
                else
                {
                }
            }
            echo"<p align=center><b>you scored $total</b>";
            }
     ?>

Problem is even if i don't select a single radio button it is giving me $result as 2.When there were only 5 questions in database there was no problem but when i added 150 questions and added random function to the query,it created problem.THANKS in advance


in your checking-

$_POST[$ans['qno']]==$ans['answer']

if no data in POST and $ans['answer'] = 0, this will return true. So you'll need to change this code to

$_POST[$ans['qno']]===$ans['answer'] 

hope it'll work


just for debugging, you can verify the form values:

  echo "<pre>".print_r($POST)."</pre>";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜