-4

i got this code from technopoints, i want to get a random question from database but i cant work this out when i put the $fetchqry = "SELECT * FROM quiz ORDER BY rand()"; it does not get the correct result i think because of the rand(). i've been trying to fix this and it doesn't work and also how can i put an incremented number per question?

                                if (isset($_POST['click']) || isset($_GET['start'])) {
                                @$_SESSION['clicks'] += 1 ;
                                $c = $_SESSION['clicks'];
                                if(isset($_POST['userans'])) { $userselected = $_POST['userans']; 

                                $fetchqry2 = "UPDATE 'quiz' SET 'userans'='$userselected' WHERE 'id'=$c-1"; 
                                $result2 = mysqli_query($db,$fetchqry2);
                                }


                                } else {
                                  $_SESSION['clicks'] = 0;
                                }

                                //echo($_SESSION['clicks']);
                                ?>


<div class="bump"><br><form><?php if($_SESSION['clicks']==0){ ?> <button class="button" name="start" float="left"><span>START QUIZ</span></button> <?php } ?></form></div>
<form action="" method="post">          
<table>
  <?php if(isset($c)) {   $fetchqry = "SELECT * FROM 'quiz' ORDER BY rand()";
        $result=mysqli_query($db,$fetchqry);
        $num=mysqli_num_rows($result);
        $row = mysqli_fetch_array($result,MYSQLI_ASSOC); }
      ?>
<tr><td><h3><br><?php echo  @$row['que'];?></h3></td></tr> <?php if($_SESSION['clicks'] && $_SESSION['clicks']){ ?>
  <tr><td><input required type="radio" name="userans" value="<?php echo $row['option 1'];?>">&nbsp;<?php echo $row['option 1']; ?><br>
  <tr><td><input required type="radio" name="userans" value="<?php echo $row['option 2'];?>">&nbsp;<?php echo $row['option 2'];?></td></tr>
  <tr><td><input required type="radio" name="userans" value="<?php echo $row['option 3'];?>">&nbsp;<?php echo $row['option 3']; ?></td></tr>
  <tr><td><input required type="radio" name="userans" value="<?php echo $row['option 4'];?>">&nbsp;<?php echo $row['option 4']; ?><br><br><br></td></tr>
  <tr><td><button class="button3" name="click" >Next</button></td></tr> <?php }  
                                  ?> 
  <form>
 <?php if($_SESSION['clicks']){ 
  $qry3 = "SELECT 'ans', 'userans' FROM 'quiz';";
  $result3 = mysqli_query($db,$qry3);
  $storeArray = Array();
  while ($row3 = mysqli_fetch_array($result3, MYSQLI_ASSOC)) {
     if($row3['ans']==$row3['userans']){
     @$_SESSION['score'] += 1 ;
   }
}

 ?>

<h2>Result</h2>
 <span>No. of Correct Answer:&nbsp;<?php echo $no = @$_SESSION['score']; 
 session_unset(); ?></span><br>
 <span>Your Score:&nbsp<?php echo $no*2; ?></span>
<?php } ?>```
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • That code doesn't have any ORDER BY RAND() query, can't help if no real code is supplied. – Triby Nov 19 '19 at 00:09
  • Because this code is perfectly running but when i change this part 'fetchqry = "SELECT * FROM 'quiz'"; and add the ORDER BY rand() this is where it goes wrong it doesn't get the right answer anymore Edit: I change it just to be clear now – Pao Aguilar Nov 19 '19 at 00:27

1 Answers1

-1

I think your sql query should be like this:

$fetchqry = "SELECT * FROM quiz ORDER BY rand() LIMIT 1";

I'm not pretty sure if you have to show all the rows into quiz or only one, if its like this please try with LIMIT 1

BaiHui
  • 9
  • 1
  • i tried it before but it did not work its still not getting the correct answer i guess the rand() messed the oder of correct answer – Pao Aguilar Nov 19 '19 at 01:23
  • I have also noticed you are not closing some form tags, can you please review your code , and check if this is the issue? Also make sure you have the session_start() function. – BaiHui Nov 19 '19 at 01:26
  • I'll check it again but it was really working fine just when i add the rand() and yes I have session_start and connected it in my sql – Pao Aguilar Nov 19 '19 at 02:12