Need help matching quiz IDs

We have 2 tables in database quiz and questions , i want to match quiz id and question_id and get result?

<?php include('config.php');
 if(isset($_SESSION['pak']) && $_SESSION['pak'] != "1")
 {
	 header("location:login.php");
 }
  ?>          
<?php
 if(isset($_POST['submit']))
 //$id = isset($_GET['id']) ? $_GET['id'] : '';
 {
    $id = $_POST['questionid'];
	$option = $_POST['option1'];
    $query2= mysql_query("SELECT * FROM questions WHERE q_id=$id")or die(mysql_error());
	//SELECT * FROM questions ORDER BY RAND() LIMIT 10
        $res= mysql_fetch_array($query2);                   
		
		
		$userid = $_SESSION['userid'];
		//print_r ($userid); exit;
	 if ($res['answer']== $option)
	
	{
		$query1 = mysql_query ("INSERT INTO quiz (question_id,user_id,useranswer) VALUES('$id','$userid','TRUE')"); 
		// print_r ("INSERT INTO quiz (question_id,user_id,answer) VALUES('$id','$userid','T')"); exit;
		// $query1 = mysql_query("INSERT INTO quiz (q_id,user_id) VALUES('$id','$_SESSION['userid']',$_POST['option1'])");
		header('location:question.php'); 
	 }
	 else
	 {
		 $query = mysql_query  ("INSERT INTO quiz (question_id,user_id,useranswer) VALUES('$id','$userid','FALSE')");
		 //print_r ("INSERT INTO quiz (question_id,user_id,answer) VALUES('$id','$userid','F')"); exit; 
		 header('location:question.php'); 
	 }
	 //if(isset($_SESSION['pak']) && $_SESSION['pak'] == "1")
     }
	 ?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title> My Web </title>
	<meta name="description" content="School App">
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/style.css">

	<!-- Optional theme -->
	<link rel="stylesheet" href="css/bootstrap-theme.min.css">
    
</head>
<body>
<!--navbar-->
<nav class="navbar navbar-default" role="navigation" style="height:77px;">
 <div class="container-fluid">
 
 
   

  <div class="navbar-header">
  <button type="button" class="navbar-toggle" data-toggle="collapse" date-target="#bs-WDM-navbar-collapse-1">
  <span class="sr-only">Toggle Navigation</span>
  <span class="icon-bar"></span>
  <span class="icon-bar"></span>
  </button>
  
 
  <a class="navbar-brand" href="index.php">  <img src="../images/logo.png" width="120px" height="43px;" /> </a>
  
  </div>
 
 <div class="collapse navbar-collapse navbar-right" id="bs-WDM-navbar-collapse-1">
  <ul class="nav navbar-nav">
    <li> <a href="index.php"> Home </a> </li>
    <li> <a href="question.php"> Question </a> </li>
    <li> <a href="logout.php"> Logout </a> </li>
    
    <!--dropdown-->
    <li class="dropdown">
     <a class="dropdown-toggle" href="" data-toggle="dropdown"> WDM DropDown <b class="caret"> </b> </a>
     <ul class="dropdown-menu"> 
       <li> <a href=""> Action1 </a> </li>
       <li> <a href=""> Action2 </a> </li>
       <li> <a href=""> Action3 </a> </li>
       <li class="divider"> </li>
       <li> <a href=""> Action4 </a> </li>
        <?php
       // if($_SESSION['pak'] == "1")
		if(isset($_SESSION['pak']) && $_SESSION['pak'] == "1")
                {
     ?>
  <li><a  href="logout.php"> Logout</a></li>
   <?php
	}
		else
			{
   ?>
   <li><a  href="login.php">  Login</a></li>
                    
    <?php
	}  
	?>
			
     
     </ul>
    
    </li>
   <h4 style="color:#090; margin-left:12px;">Welcome "<span style="color:#0F3; font-size:24px"><?php echo $_SESSION['name'];?> </span>" </h4>
           
  
  </ul>
 
 </div>

 </div>
</nav>
<!--navbar-->
<?php
if (isset($_GET['msg']))
{
	echo $_GET['msg'];	
}
?>
<div class="container" style="background-color:#CCC">
<div class="row" style="margin-top:12px;">
<form action="#" method="post">
<?php 
           $userid = $_SESSION['userid'];
	$sql_query ="SELECT * FROM quiz WHERE user_id='$userid'";
	//print_r($sql_query); 
  $result2 = mysql_query($sql_query) or die(mysql_error());
  //print_r($result2); exit;
  //$pq = mysql_query("SELECT * FROM event WHERE event_id='$id'");
  $num = mysql_num_rows($result2);
     
	 if($num <10 )   
	 { 
	 //query "question_id[<?php echo $question_id;
//count
//if()
//{
?>
 <center>  <h5> Questioner </h5> </center>
  <?php
       $j = 1;
	    $query = mysql_query("SELECT * FROM questions ORDER BY RAND()");
          $rec = mysql_fetch_array($query);
		  {
  ?>                                             
        <div class="form-group">
                      <input type="text" class="form-control" name="question" value="<?php echo $rec['question'];?>" 
                      placeholder="How many digits of the DNIC (Data Network Identification Code) identify the country?">
                    </div>
                   <!-- <div class="form-group">
                      <input type="radio" class="form-control" name="type" value=""  />
                    </div><input type="hidden" name="type" value="1"> </input>  <?php //if($res['type']==1){echo 'selected' ;} ?> -->
                    
                    <input type="hidden" name="questionid" value="<?php echo $rec['q_id'];?>"> 
                    
                     <?php if( $rec['type']==1) { ?>
                     
                    <div class="form-group"> <input type="radio" name="option1" value="A"> <?php echo $rec['option1'];?> </div>
                    <div class="form-group"> <input type="radio" name="option1" value="B"> <?php echo $rec['option2'];?> </div>
                    <div class="form-group"> <input type="radio" name="option1" value="C"> <?php echo $rec['option3'];?></div>
                    <div class="form-group"> <input type="radio" name="option1" value="D"> <?php echo $rec['option4'];?> </div>
                    
                   <?php  }
				   else {
					           ?>
                    <div class="form-group"> <input type="hidden" name="type" value="2" > </input> </div>
                     <div class="form-group">  <input type="radio" name="option1" value="TRUE"> TRUE </div>
                     <div class="form-group">  <input type="radio" name="option1" value="FALSE"> FALSE </div>
                     <?php } ;?>
                     <!--   $usercheckedanswer me save kra do jo radio button me check ho raha ho

Aur jab sql se questions lekr arhe ho to answere kisi variable me store kra do
Echo while k loop se bahir krao

Phr button click me in dono ko match krlo 
     Ek varible me jo user radio button check kr raha hai, aur dusra jo sql se question utha rhe uska añswere
   -->
                    
            <?php
			$j++;
				}
			?>
                    <br>
                    
                    
                    <center> <button type="submit" name="submit" value="submit" class="btn btn-success btn-block"> Submit </button> </center>
 
 
 
                  </form>
 <?php
}
else
{  
echo "<br><br>Test Completed"; 

    //("SELECT * FROM login left join user_roles on login.id=uid WHERE rid=38  AND username='$username'  AND email='$email'");
	             //$qq = "SELECT * FROM quiz WHERE user_id='$userid'";
				  // $id = $_GET['id'];
				  
				//echo $res; exit;


 ?>  
 <br>  <br>  <br>  <br>   
 <table class="table table-bordered table-stripped table-hover">
    <thead>
      <tr>
        <th> Question No : </th>
        <th> Question : </th>
        <th> Correct Answer : </th>
        <th> User Answer : </th>
      </tr>
      
    </thead>
    <tbody>
    <?php  
	$userid = $_SESSION['userid'];
			$j=1;
			   $q5 = "SELECT * FROM quiz left join questions on question_id = q_id WHERE user_id='$userid'";  
			
			   //$qq= mysql_query("SELECT * FROM quiz left join questions on questions.quizid=q_id WHERE user_id='104'");
			   // sir che dae kum task mala nun saba rakai , za pa de na rasam , za pora try kom kho bia hum, lag code kho de rata khai k mushkila na we nu
			  //"SELECT * FROM login left join user_roles on login.id=uid WHERE rid=38  AND username='$username'  AND email='$email' AND password='$password'"
			     $q = mysql_query($q5);
			while($res = mysql_fetch_array($q))  
			{
				?>
      <tr>
         <td><?php echo $j;?></td>   
        <td> <?php echo $res['question'];?> </td>
        <td> <?php echo $res['answer'];?> </td>
        <td> <?php echo $res['useranswer'];?> </td>
        <td> </td>
      </tr>
      <?php  $j++; }  ?>
      
       <?php 
	  $score=0; 
	  $correctanswer = $res['answer'];
	  // print_r($correctanswer); exit;
	  $useranswer = $res['useranswer'];
	  if($correctanswer == $useranswer) 
	  { 
	    ?>
     <tr> <th> Result is : </th> </tr>
         
      
	  <?php echo $score++;
	  }
	  ?>
      
    </tbody>
  </table>
  <?php } ?>
</div>
 
 </div>

	<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
	<!-- Latest compiled and minified JavaScript -->
   
	<script src="js/bootstrap.min.js"></script>
</body>
</html>

Can you expand on that? The PHP code you posted seems to retrieve the users answer, check if it’s the same as the answer in the questions table, and record in the quiz table whether the user got the answer correct or incorrect, which seems to be what you’re asking. If not, please expand on the question and how it relates to the code above.

Also, some other things:

When you post the PHP code (and the HTML, for that matter) it’s a good idea to clean it up, get rid of all the stuff you’ve commented out, save us wading through all the code that isn’t being used unless there’s a specific reason for it being there.

Stop using the old-style mysql_ calls to access your database. They’ve been removed from the language now after 10+ years of being deprecated. If this is new code, you should look at using either mysqli or (my preference) PDO instead.

1 Like

I don’t want to just copy and paste code, but I have been developing a trivia game for sometime now. Here is the website:
https://www.interactivetrivia.xyz/

There’s a Github webpage link of that website that gives all the files necessary to run. It might be a little above you level, but maybe it will give some clues on how to do it? Feel free to download it and play with the code, if you wish to do.

I haven’t gotten to that part of inserting or editing questions, but I’ll be that soon and updating my website when I do.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.