Passing value onto script and checking if its the right ans

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bumdeal2
Forum Newbie
Posts: 11
Joined: Thu Apr 01, 2010 11:12 am

Passing value onto script and checking if its the right ans

Post by bumdeal2 »

Hey guys i have 2 scripts PlayGame.php and Grad.php.

What i need to do is pass the variable that is the correct answer onto the grad.php and if its correct it gives you a percentage however when it comes ot the correcting in grad it does not seem to be getting the value!Help would be truely appreciated?



Code: Select all

print("</head>");
		print('<BODY onLoad="goNewWin()">');
		print('<form action="grad.php?qid=$qid&quesid=$quesid&totalCorrect=$totalCorrect&taken=$taken" method="post" id="quiz">');
		print('<table class="report" align=left width=100%>');

		print('<tr><td>&nbsp;</td></tr>');


		$result=mysql_query($sql) or die ('Unable execute the query');
;



		if(mysql_numrows($result))
		{
				$row = mysql_fetch_row($result);
				{

				$questid=$row[0];
				$quesid=$row[1];
				$question=$row[2];
				$ranswer=$row[3];
				$ranswer1=$row[4];
				$ranswer2=$row[5];
				$answer=$row[6];
				$qid=$row[7];

$answer = array($ranswer,$ranswer1,$ranswer2,$answer);
shuffle ($answer);



print('<body>');



	print('<tr><td >'.$quesid.'&nbsp;&nbsp;&nbsp;'.$question.'</td>'.$Quiz_Topic.'</tr>');

	print('<input type= hidden name=qid value="'.$qid.'">');

	print('<input type= hidden name=totalCorrect value="'.$totalCorrect.'">');

	print('<input type= hidden name=quesid value="'.$quesid.'">');

	print('<input type= hidden name=score value="'.$score.'">');

	print('<input type= hidden name=taken value="'.$taken.'">');

	print('<tr><td><input type="radio" name="answer" id="answer" value="'.$answer[0].'">&nbsp;&nbsp;&nbsp;'.$answer[0].'</td></tr>');

	print('<tr><td><input type="radio" name="answer" id="answer" value="'.$answer[1].'">&nbsp;&nbsp;&nbsp;'.$answer[1].'</td></tr>');

	print('<tr><td><input type="radio" name="answer" id="answer" value="'.$answer[2].'">&nbsp;&nbsp;&nbsp;'.$answer[2].'</td></tr>');

	print('<tr><td><input type="radio" name="answer" id="answer" value="'.$answer.'">&nbsp;&nbsp;&nbsp;'.$answer[3].'</td></tr>');

	print('<tr><td>&nbsp;<input type=hidden name=answer id="answer" value="'.$answer.'"><input type= hidden name=qnumber value="'.$question.'"></tr>');


	}
	print('<tr><td colspan=2><input type=submit name=submit value="Submit" ><input type=reset name=reset value="Clear" ></td></tr></table>');


	print('</form>');

	}
else
	{

	 echo "<div id='results'>Your result is:  $score%</div>";
	print('<input type =hidden name=score value="'.$score.'">');
	print('</form>');

	}

Grad.php

Code: Select all

<?php


include('auth.php');
include('database_access_param.php');
import_request_variables("pgc","");

$quesid = $_POST['quesid'];
$qid= $_POST['qid'];
$answer = $_POST['answer'];
$totalCorrect = $_POST['totalCorrect'];
$answer = $_POST['answer'];
$score = $_POST['score'];



$exist=0;
$cday= date('d');
$cmonth= date('m');
$cyear= date('Y');


$dbcurrent_date=$cyear.'-'.$cmonth.'-'.$cday;



  $db_link=mysql_connect($hostname, $dbuser, $dbpassword) or die("Unable to connect to the server!");

		mysql_select_db($dbname) or die("Unable to connect to the database.");


		$ssql = "SELECT scoreid,qid,username,score,taken,qdate FROM scores WHERE
                username = '".$user."' AND
                qid = '$qid'";





if($quesid < 11)

{
$answer1 = $_POST["'$answer'"];

if ($answer1 == "'$answer'")
{
 $totalCorrect++;



	    $score = $totalCorrect/$quesid*100;

	    $quesid++;



header("location:PlayGame.php?quesid=$quesid&qid=$qid&totalCorrect=$totalCorrect&score=$score&taken=$taken");



}

else if ($answer1 != "answer")
{

 $score =$totalCorrect/$quesid*50;

	    $quesid++;



header("location:PlayGame.php?quesid=$quesid&qid=$qid&totalCorrect=$totalCorrect&score=$score&taken=$taken");
}









 if($quesid=11)
{


$taken=1;



$isql="insert into scores(scoreid,username,score,qid,taken,qdate) values
			                         (0, '".$user."',".$score.",".$qid.",".$taken.",'".$dbcurrent_date."')";


        echo "<div id='results'>Your result is:  $subTotal%</div>";

						if(!mysql_query($isql))
						{
						echo mysql_errno() . "";
						echo mysql_error() . "<BR>";
						}


}
}



include('PlayGame.php');


?>
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: Passing value onto script and checking if its the right

Post by roders »

Oh my where do i start. Hmmmmm.... ok
1. look at the line where it says mysql_numrows in the first part that you posted this is wrong should be mysql_num_rows.
2. You're radio button you're displaying some $answer[0],$answer[1],$answer[2],then you're go back to $answer which is the array itself. so i fink you might want to change that yo $answer[3]
3. Am not gonna mention that the coding looks very wrong :D .
4. 2nd part of your script what on earth is this

Code: Select all

$answer1 = $_POST["'$answer'"];
if ($answer1 == "'$answer'")
my god man.
I mean there's newbie and newbieeeeeeeee, don't mind helping you but you need to correct all this before i do.
My suggestion get the php manual up read it understand the concept of coding in php then start off again.
bumdeal2
Forum Newbie
Posts: 11
Joined: Thu Apr 01, 2010 11:12 am

Re: Passing value onto script and checking if its the right

Post by bumdeal2 »

Sry bout that, this should look slightley better!!!Hopefully :-P

PlayGame.php

Code: Select all

<?php



include('database_access_param.php');
include('auth.php');


$right=0;

$qid=$_POST['qid'];
$quesid=$_POST['quesid'];
$totalCorrect=$_POST['totalCorrect'];
$score=$_POST['score'];
$taken=$_POST['taken'];
import_request_variables("pgc","");


if(isset($_GET['quesid']))
 {

   $quesid=$_GET['quesid'];
}


if(isset($_GET['qid']))
{

   $qid=$_GET['qid'];

}
if(isset($_GET['totalCorrect']))
{

   $totalCorrect=$_GET['totalCorrect'];

}
if(isset($_GET['score']))
{

   $score=$_GET['score'];

}
if(isset($_GET['taken']))
{

   $taken=$_GET['taken'];

}

    mysql_connect( $hostname, $dbuser, $dbpassword)
        or die ( 'Unable to connect to server' );



    mysql_select_db($dbname )
        or die ( 'Unable to select database' );


    $sql = "SELECT * FROM questiona WHERE qid = '$qid' && quesid = '$quesid' ";




		print("</head>");
		print('<BODY onLoad="goNewWin()">');
		print('<form action="grad.php?qid=$qid&quesid=$quesid&totalCorrect=$totalCorrect&taken=$taken" method="post" id="quiz">');
		print('<table class="report" align=left width=100%>');

		print('<tr><td>&nbsp;</td></tr>');


		$result=mysql_query($sql) or die ('Unable execute the query');
;



		if(mysql_numrows($result))
		{
				$row = mysql_fetch_row($result);
				{

				$questid=$row[0];
				$quesid=$row[1];
				$question=$row[2];
				$ranswer=$row[3];
				$ranswer1=$row[4];
				$ranswer2=$row[5];
				$answer=$row[6];
				$qid=$row[7];

$answer = array($ranswer,$ranswer1,$ranswer2,$answer);
shuffle ($answer);



print('<body>');



	print('<tr><td >'.$quesid.'&nbsp;&nbsp;&nbsp;'.$question.'</td>'.$Quiz_Topic.'</tr>');

	print('<input type= hidden name=qid value="'.$qid.'">');

	print('<input type= hidden name=totalCorrect value="'.$totalCorrect.'">');

	print('<input type= hidden name=quesid value="'.$quesid.'">');

	print('<input type= hidden name=score value="'.$score.'">');

	print('<input type= hidden name=taken value="'.$taken.'">');

	print('<tr><td><input type="radio" name="answer" id="answer" value="'.$answer[0].'">&nbsp;&nbsp;&nbsp;'.$answer[0].'</td></tr>');

	print('<tr><td><input type="radio" name="answer" id="answer" value="'.$answer[1].'">&nbsp;&nbsp;&nbsp;'.$answer[1].'</td></tr>');

	print('<tr><td><input type="radio" name="answer" id="answer" value="'.$answer[2].'">&nbsp;&nbsp;&nbsp;'.$answer[2].'</td></tr>');

	print('<tr><td><input type="radio" name="answer" id="answer" value="'.$answer[3].'">&nbsp;&nbsp;&nbsp;'.$answer[3].'</td></tr>');

	print('<tr><td>&nbsp;<input type=hidden name=answer id="answer" value="'.$answer[3].'"><input type= hidden name=qnumber value="'.$question.'"></tr>');


	}
	print('<tr><td colspan=2><input type=submit name=submit value="Submit" ><input type=reset name=reset value="Clear" ></td></tr></table>');


	print('</form>');

	}
else
	{

	 echo "<div id='results'>Your result is:  $score%</div>";
	print('<input type =hidden name=score value="'.$score.'">');
	print('</form>');

	}


?>
And heres Grad.php second time lucky hopefully.

Code: Select all

<?php


include('auth.php');
include('database_access_param.php');
import_request_variables("pgc","");

$quesid = $_POST['quesid'];
$qid= $_POST['qid'];
$answer = $_POST['answer'];
$totalCorrect = $_POST['totalCorrect'];
$answer = $_POST['answer'];
$score = $_POST['score'];



$exist=0;
$cday= date('d');
$cmonth= date('m');
$cyear= date('Y');


$dbcurrent_date=$cyear.'-'.$cmonth.'-'.$cday;



  $db_link=mysql_connect($hostname, $dbuser, $dbpassword) or die("Unable to connect to the server!");

		mysql_select_db($dbname) or die("Unable to connect to the database.");


		$ssql = "SELECT scoreid,qid,username,score,taken,qdate FROM scores WHERE
                username = '".$user."' AND
                qid = '$qid'";





if($quesid < 11)

{
$answer1 = $_POST["answer"];

if ($answer1 == "answer")
{
 $totalCorrect++;



	    $score = $totalCorrect/$quesid*100;

	    $quesid++;



header("location:PlayGame.php?quesid=$quesid&qid=$qid&totalCorrect=$totalCorrect&score=$score&taken=$taken");



}

else if ($answer1 != "answer")
{

 $score =$totalCorrect/$quesid*50;

	    $quesid++;



header("location:PlayGame.php?quesid=$quesid&qid=$qid&totalCorrect=$totalCorrect&score=$score&taken=$taken");
}









 if($quesid=11)
{


$taken=1;



$isql="insert into scores(scoreid,username,score,qid,taken,qdate) values
			                         (0, '".$user."',".$score.",".$qid.",".$taken.",'".$dbcurrent_date."')";


        echo "<div id='results'>Your result is:  $subTotal%</div>";

						if(!mysql_query($isql))
						{
						echo mysql_errno() . "";
						echo mysql_error() . "<BR>";
						}


}
}



include('PlayGame.php');


?>
bumdeal2
Forum Newbie
Posts: 11
Joined: Thu Apr 01, 2010 11:12 am

Re: Passing value onto script and checking if its the right

Post by bumdeal2 »

Sry bout that, this should look slightley better!!!Hopefully :-P

PlayGame.php

Code: Select all

<?php



include('database_access_param.php');
include('auth.php');


$right=0;

$qid=$_POST['qid'];
$quesid=$_POST['quesid'];
$totalCorrect=$_POST['totalCorrect'];
$score=$_POST['score'];
$taken=$_POST['taken'];
import_request_variables("pgc","");


if(isset($_GET['quesid']))
 {

   $quesid=$_GET['quesid'];
}


if(isset($_GET['qid']))
{

   $qid=$_GET['qid'];

}
if(isset($_GET['totalCorrect']))
{

   $totalCorrect=$_GET['totalCorrect'];

}
if(isset($_GET['score']))
{

   $score=$_GET['score'];

}
if(isset($_GET['taken']))
{

   $taken=$_GET['taken'];

}

    mysql_connect( $hostname, $dbuser, $dbpassword)
        or die ( 'Unable to connect to server' );



    mysql_select_db($dbname )
        or die ( 'Unable to select database' );


    $sql = "SELECT * FROM questiona WHERE qid = '$qid' && quesid = '$quesid' ";




		print("</head>");
		print('<BODY onLoad="goNewWin()">');
		print('<form action="grad.php?qid=$qid&quesid=$quesid&totalCorrect=$totalCorrect&taken=$taken" method="post" id="quiz">');
		print('<table class="report" align=left width=100%>');

		print('<tr><td>&nbsp;</td></tr>');


		$result=mysql_query($sql) or die ('Unable execute the query');
;



		if(mysql_numrows($result))
		{
				$row = mysql_fetch_row($result);
				{

				$questid=$row[0];
				$quesid=$row[1];
				$question=$row[2];
				$ranswer=$row[3];
				$ranswer1=$row[4];
				$ranswer2=$row[5];
				$answer=$row[6];
				$qid=$row[7];

$answer = array($ranswer,$ranswer1,$ranswer2,$answer);
shuffle ($answer);



print('<body>');



	print('<tr><td >'.$quesid.'&nbsp;&nbsp;&nbsp;'.$question.'</td>'.$Quiz_Topic.'</tr>');

	print('<input type= hidden name=qid value="'.$qid.'">');

	print('<input type= hidden name=totalCorrect value="'.$totalCorrect.'">');

	print('<input type= hidden name=quesid value="'.$quesid.'">');

	print('<input type= hidden name=score value="'.$score.'">');

	print('<input type= hidden name=taken value="'.$taken.'">');

	print('<tr><td><input type="radio" name="answer" id="answer" value="'.$answer[0].'">&nbsp;&nbsp;&nbsp;'.$answer[0].'</td></tr>');

	print('<tr><td><input type="radio" name="answer" id="answer" value="'.$answer[1].'">&nbsp;&nbsp;&nbsp;'.$answer[1].'</td></tr>');

	print('<tr><td><input type="radio" name="answer" id="answer" value="'.$answer[2].'">&nbsp;&nbsp;&nbsp;'.$answer[2].'</td></tr>');

	print('<tr><td><input type="radio" name="answer" id="answer" value="'.$answer[3].'">&nbsp;&nbsp;&nbsp;'.$answer[3].'</td></tr>');

	print('<tr><td>&nbsp;<input type=hidden name=answer id="answer" value="'.$answer[3].'"><input type= hidden name=qnumber value="'.$question.'"></tr>');


	}
	print('<tr><td colspan=2><input type=submit name=submit value="Submit" ><input type=reset name=reset value="Clear" ></td></tr></table>');


	print('</form>');

	}
else
	{

	 echo "<div id='results'>Your result is:  $score%</div>";
	print('<input type =hidden name=score value="'.$score.'">');
	print('</form>');

	}


?>
And heres Grad.php second time lucky hopefully.

Code: Select all

<?php


include('auth.php');
include('database_access_param.php');
import_request_variables("pgc","");

$quesid = $_POST['quesid'];
$qid= $_POST['qid'];
$answer = $_POST['answer'];
$totalCorrect = $_POST['totalCorrect'];
$answer = $_POST['answer'];
$score = $_POST['score'];



$exist=0;
$cday= date('d');
$cmonth= date('m');
$cyear= date('Y');


$dbcurrent_date=$cyear.'-'.$cmonth.'-'.$cday;



  $db_link=mysql_connect($hostname, $dbuser, $dbpassword) or die("Unable to connect to the server!");

		mysql_select_db($dbname) or die("Unable to connect to the database.");


		$ssql = "SELECT scoreid,qid,username,score,taken,qdate FROM scores WHERE
                username = '".$user."' AND
                qid = '$qid'";





if($quesid < 11)

{
$answer1 = $_POST["answer"];

if ($answer1 == "answer")
{
 $totalCorrect++;



	    $score = $totalCorrect/$quesid*100;

	    $quesid++;



header("location:PlayGame.php?quesid=$quesid&qid=$qid&totalCorrect=$totalCorrect&score=$score&taken=$taken");



}

else if ($answer1 != "answer")
{

 $score =$totalCorrect/$quesid*50;

	    $quesid++;



header("location:PlayGame.php?quesid=$quesid&qid=$qid&totalCorrect=$totalCorrect&score=$score&taken=$taken");
}









 if($quesid=11)
{


$taken=1;



$isql="insert into scores(scoreid,username,score,qid,taken,qdate) values
			                         (0, '".$user."',".$score.",".$qid.",".$taken.",'".$dbcurrent_date."')";


        echo "<div id='results'>Your result is:  $subTotal%</div>";

						if(!mysql_query($isql))
						{
						echo mysql_errno() . "";
						echo mysql_error() . "<BR>";
						}


}
}



include('PlayGame.php');


?>
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: Passing value onto script and checking if its the right

Post by roders »

Ok gud much better.
In the 2nd part of your script, your checking if the value being post is equal to the string/word answer, is that correct???

Code: Select all

if ($answer1 == "answer")
Shouldn't you be checking if the value being posted is the same as the correct answer in your database?
You also have the following twice in your script

Code: Select all

$answer = $_POST['answer'];
katierosy
Forum Commoner
Posts: 27
Joined: Wed Apr 07, 2010 8:39 am

Re: Passing value onto script and checking if its the right

Post by katierosy »

Please use hidden field to post values in instead of param meter values.Please re-look into the code,It must work there is no such identifiable cause , this script not to work.
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: Passing value onto script and checking if its the right

Post by roders »

Well look closely and you will find out that their are a lot of mistake in his codes. And that's why am trying to help this person figuring out where they are and avoid them in the future.
And just a simple example check this statement.

Code: Select all

if($quesid=11)
Do you see where the mistake is??
bumdeal2
Forum Newbie
Posts: 11
Joined: Thu Apr 01, 2010 11:12 am

Re: Passing value onto script and checking if its the right

Post by bumdeal2 »

Sry that should be ==, instead of =, so if($quesid==11), and i just cant get it to work,
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Passing value onto script and checking if its the right

Post by omniuni »

At this point, I would recommend you start a new file, and break down the code. Re-code it bit by bit and make sure that each piece works. Start simple. Pass one value from one page to the other without any other stuff. Then start adding more things. Make a query and display the results, don't worry about formatting them. Finally, you should be able to narrow down what mistakes your're making, and what specifically you need help with.
bumdeal2
Forum Newbie
Posts: 11
Joined: Thu Apr 01, 2010 11:12 am

Re: Passing value onto script and checking if its the right

Post by bumdeal2 »

I know exactly what works in the program, its just i dont know how to get the answer in the grad file to compare with the right answer, so its that section of code all the other values being apssed are functioning ok.....
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: Passing value onto script and checking if its the right

Post by roders »

Create another mysql query to retreive the correct with the answer id being send from the form.
something like that

Code: Select all

$query="select * from answertable where answer_id = ".$_POST['answer']." and correctanswerfield='Y'";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
if(mysql_num_rows($result) > 0)
{
//Insert what happens when the answer is correct here.
}else{
//insert what happens when the answer is wrong here.
}
bumdeal2
Forum Newbie
Posts: 11
Joined: Thu Apr 01, 2010 11:12 am

Re: Passing value onto script and checking if its the right

Post by bumdeal2 »

Thanks guys for all the help, even if my code looks horrendous:-) all your help has been appreciated and thanks for your time.
Post Reply