随机问题选择并将其存储到db

时间:2016-02-15 07:46:45

标签: php mysql random

我正在进行一个测验页面,每次随机选择20个问题。我以前使用ORDER BY RAND()LIMIT 20来选择随机问题,但是我无法将它存储到我在其他页面中的数据库。现在我我正在使用" quesid"而不是RAND()。我想以正确的顺序存储它。任何人请帮助我。

Quiz.php

<?php  
$rs=mysql_query("select * from question where testid=$tid order by quesid ",$cn) or die(mysql_error());

if($_SESSION[qn]>mysql_num_rows($rs)-1)
{
unset($_SESSION[qn]);
echo "<h1 class=head1>Some Error  Occured</h1>";
session_destroy();
echo "Please <a href=UserHome.php> Start Again</a>";

exit;
}
        $n=0;
        while($row= mysql_fetch_row($rs)){?>
        <form name="myfm" id="myfm" method="post" action="QuizSub.php">
        <table width=100%> <tr> <td width=30><td></td></td></tr> <table border=0>
        <?php $n=$n+1; ?>
        <tr><td>Question <?php echo $n.")  "; echo $row[2]; ?></td></tr>
        <tr><td class=style8>A. <input type="radio" name="ques['<?php echo $n; ?>'][]" value=1><?php echo $row[3]; ?></td></tr>
        <tr><td class=style8>B. <input type="radio" name="ques['<?php echo $n; ?>'][]" value=2><?php echo $row[4];?></td></tr>
        <tr><td class=style8>C. <input type="radio" name="ques['<?php echo $n; ?>'][]"  value=3><?php echo $row[5];?></td></tr>
        <tr><td class=style8>D. <input type="radio" name="ques['<?php echo $n; ?>'][]"  value=4><?php echo $row[6];?></td></tr>

    <?php 
        }
        echo "<tr><td><input type=submit name=submit id='result' value='Get Result'></form>";
        ?>
        </table></table>
        </form>

Quizsub.php

        $rs=mysql_query("select * from question where testid=$tid order by quesid ",$cn) or die(mysql_error());
        if($submit=='Get Result')
        { 
        $_SESSION[trueans]=0;
        $_SESSION[qn]=0;


        $ans=array();



        $questions = $_POST["ques"];
        foreach ($questions as $q) {  
        $_SESSION[qn]=$_SESSION[qn]+1;    
        $ans=$q[0][$i];
        $rw=mysql_fetch_row($rs);
        mysql_query("insert into useranswer(sessid, testid, ques, ans1,ans2,ans3,ans4,correctans,yourans) values ('".session_id()."',       $tid,'$rw[2]','$rw[3]','$rw[4]','$rw[5]', '$rw[6]','$rw[7]','$ans')") or die(mysql_error());

        if($ans==$rw[7])
                {
                        $_SESSION[trueans]=$_SESSION[trueans]+1;
                }
                                                }
            echo "<h1 class=head1> Result</h1>";
            echo "<Table align=center><tr class=tot><td>Total Question<td> $_SESSION[qn]";
            echo "<tr class=tans><td>True Answer<td>".$_SESSION[trueans];
            $w=$_SESSION[qn]-$_SESSION[trueans];
            echo "<tr class=fans><td>Wrong Answer<td> ". $w;
            echo "</table>";
            mysql_query("insert into result(login,testid,testdate,score) values('$login','$tid','".date("Y-m-d")."','".$_SESSION[trueans]."' )"); //or die(mysql_error());
            //echo "<h1 align=center><a href=Review.php> Review Question</a> </h1>";
            unset($_SESSION[qn]);
            unset($_SESSION[sid]);
            unset($_SESSION[tid]);
            unset($_SESSION[trueans]);
            exit;
    }

0 个答案:

没有答案