-1

Im trying to run the php file but its giving the empty variables, it quiz website in which user answer i am tring to compare the user response with database value but giving no results

here is the answer.php that calls the function:

<?php
require("functions.php");

$ss=answer($_POST);

?>

here is function.php file

<?php
include('dbConnect.php');
?>

<?php

function answer($data){

    // creating query to get category id
    $sql="select * from category";
    $result=mysql_query($sql);
    while($row=mysql_fetch_array($result)){
        $cat=$row['cat_id'];
    }

    // another query to select table and compare values
    $response="select q_id, ans from questions where cat_id='$cat'" ;
    $right_answer=0;
    $wrong_answer=0;
    $unanswered=0;
    $result = mysql_query($response);

    while($qust=mysql_fetch_array($result)){

        if($qust['ans']==$_POST[$row['q_id']]){
            $right_answer++;
        } else if($_POST[$row['q_id']]=="no_attempt"){
            $unanswered++;
        } else {
            $wrong_answer++;
        }
   }
}
echo "right_answer : ". $right_answer."<br>";
echo "wrong_answer : ". $wrong_answer."<br>";
echo "unanswered : ". $unanswered."<br>";
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Maliha Khan
  • 15
  • 1
  • 6
  • `while($row=mysql_fetch_array($result)){$cat=$row['cat_id'];}` will give you the LAST value in the recordset – Professor Abronsius Aug 19 '16 at 22:54
  • You shouls migrate from the MySQL Extension to PDO – Oncodeeater Aug 19 '16 at 22:55
  • The code that uses `$cat` should be inside the first `while` loop. But it would be better to join the queries instead of doing nested loops. – Barmar Aug 19 '16 at 23:07
  • Google "php variable scope" – Charlotte Dunois Aug 19 '16 at 23:14
  • Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) its really pretty easy – RiggsFolly Aug 19 '16 at 23:29
  • Some sensible code indentation would be a good idea. It help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Aug 19 '16 at 23:29

1 Answers1

0

if you indent your code sensibly, it help the readability but more importantly the debuggability.

When indented well you can quickly see that the last 3 lines of your answer function are outside the function scope and inside the global scope. When you call the

require 'function.php` 

it therefore runs these last 3 lines straight away,

echo "right_answer : ". $right_answer."<br>";
echo "wrong_answer : ". $wrong_answer."<br>";
echo "unanswered : ". $unanswered."<br>";

before you run

$ss=answer($_POST);

and therefore before you have actually create the 3 variables and assigned them a value.

Amend the code like this and it will not do that

<?php
include('dbConnect.php');

function answer($data){

    // creating query to get category id
    $sql="select * from category";
    $result=mysql_query($sql);
    while($row=mysql_fetch_array($result)){
        $cat=$row['cat_id'];
    }

    // another query to select table and compare values
    $response="select q_id, ans from questions where cat_id='$cat'" ;
    $right_answer=0;
    $wrong_answer=0;
    $unanswered=0;
    $result = mysql_query($response);

    while($qust=mysql_fetch_array($result)){

        if($qust['ans']==$_POST[$row['q_id']]){
            $right_answer++;
        } else if($_POST[$row['q_id']]=="no_attempt"){
            $unanswered++;
        } else {
            $wrong_answer++;
        }
    }
    echo "right_answer : " . $right_answer."<br>";
    echo "wrong_answer : " . $wrong_answer."<br>";
    echo "unanswered : "   . $unanswered."<br>";
}
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149