siby xavier
siby xavier

Reputation: 79

Random data from database

I am working on a quiz page which take questions from a table and store result in another table.Its working fine.But I want random questions from table and store it.ques[] is used to store all answers. please help me to do that. Here is my Quiz page

Quiz.php

<?php   
error_reporting(E_ALL &~ E_NOTICE &~ E_DEPRECATED); 
$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> 

Answer storing page

QuizSub.php

    <?php 
    error_reporting(E_ALL &~ E_NOTICE &~ E_DEPRECATED);
    $query="select * from question";

        $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());
  ?>

I tried RAND() for this but question and answer mismatch occurs.

 $rs=mysql_query("select * from question where testid=$tid order by RAND()",$cn)

Please help me to find a solution.. Thankyou...

Upvotes: 1

Views: 69

Answers (1)

Dimag Kharab
Dimag Kharab

Reputation: 4519

Pass question id as (hidden), and while inserting in answer page , do a where based on question id

    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') 
    WHERE quesid = ".$rw['quesid']. ") or die(mysql_error());

Upvotes: 2

Related Questions