Jump to content

Test script not working


Andrew R

Recommended Posts

<?php  

$db = mysql_connect("server", "user", "pass");
mysql_select_db("database",$db); 

if ($useraction != 'checkit') {

if($row_entrytestdata['taken'] == "No") {	//Security start

$updatedtest = "UPDATE entrypendingtest SET taken = 'Yes' where id = '$testid'";
$resultupdatedtest = mysql_query($updatedtest);

$answer = array();
$question = array();
$qnum = array();
$orgid = array();
$i = 0;
$query = "SELECT * from entrytest ORDER by RAND() LIMIT 20";
$result = mysql_query($query, $db);

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
$answer[$i] = $row[5];
$question[$i] = $row['1'];
$qnum[$i] = $i;
$orgid[$i] = $row['id'];
$ans1[$i] = $row[2];
$ans2[$i] = $row[3];
$ans3[$i] = $row[4];	
$i++;
}
$user_answer = array();
$i = 0;
$j = 1;
print "<FORM ACTION='$PHP_SELF' METHOD='post'><BR>\n";
while ($j <= 20)  {
	print "Question #" . $j . ': ' . "<b>".$question[$i]."</b><BR>\n";
	print "<INPUT TYPE='radio' NAME='user_answer[" . $i . "]'  VALUE='A'>".$ans1[$i]."<BR>\n";
	print "<INPUT TYPE='radio' NAME='user_answer[" . $i . "]'  VALUE='B'>".$ans2[$i]."<BR>\n";
	print "<INPUT TYPE='radio' NAME='user_answer[" . $i . "]'  VALUE='C'>".$ans3[$i]."<P><P>\n";
	$i++;
	$j++;
}

//print "<INPUT TYPE='hidden' NAME='qkey' VALUE='$questionkey'>\n";	
$ii=0;
while ($ii<=19) {
	print "<input type='hidden' name='answer[$ii]' value='$answer[$ii]'>\n";
	print "<input type='hidden' name='orgid[$ii]' value='$orgid[$ii]'>\n";
	$ii++;
} 
print "<input type='hidden' name='email2' value='$email'>\n";
print "<input type='hidden' name='testid2' value='$testid'>\n";
print "<input type='hidden' name='name2' value='$name'>\n";
print "<INPUT TYPE='hidden' NAME='useraction' VALUE='checkit'>\n"; 
print "<INPUT TYPE='submit' NAME='submitted' VALUE='Submit Test'>\n";
print "</FORM><br>\n";

} //Security End

else {
echo "Sorry, this test has expired.";

}	

} else {     // <!-- if submitted, check the answers on the form -->


$query_entrytestcomplted = "SELECT * FROM entrytestcompleted where email = '$email2'";
$entrytestcomplted = mysql_query($query_entrytestcomplted, $conn_abrv) or die(mysql_error());
$row_entrytestcomplted = mysql_fetch_assoc($entrytestcomplted);
$totalRows_entrytestcomplted = mysql_num_rows($entrytestcomplted);

if($totalRows_entrytestcomplted != "0") {

echo "This test has already been submitted. Sorry champ";

}
else {


$i=0;
$correct = 0;
$incorrect = 0;
foreach ($_POST['orgid'] as $k =>$v)  {
$orgid[$k] = $v;
}

$k = 0;
$m = 0;
	while ($k <= 19)  {
		if ($_POST['user_answer'][$k] == $answer[$k])  {
			$correct++;
			unset ($_POST['user_answer'][$k]);
			unset ($orgid[$k]);
		} else {
			$incorrect++;
		}
		$k++;
	}


	$percent = $correct*5;
echo "<br><br>";
print "You answered $correct questions correctly and $incorrect questions incorrectly. This eqautes to $percent%<br>";
if($correct >= 14)  { 
	echo "<br><br><font color=#006600><b>Well done! You passed the entry test</b></font>. Heres our <a href=\"applicationform.php?testid=$testid2\">application form</a><br><br>";
} elseif   ($correct > 10) {
	echo "<br><br>You fell a bit short of a passing grade this time.  You were close to our cutoff point so dont be afraid to try again<br><br>";
} else {
		echo "<br><br>Sorry, unfortunately you havent reached the high standard we require<br><br>";
} 

}



$entrytestcomplete = "INSERT into entrytestcompleted VALUES ('$testid2', NOW(), '$email2', 'Yes', '$correct', '$percent', '$name2')";

$resultcompletetest = mysql_query($entrytestcomplete);

} // original if





?>

 

Hi there.  The above script pulls a random test from the database and displays 20 questions from the database.  Once the test is submitted it checks from the database to see if the answers are correct and also inserts ‘Yes’ into the entrypending test database.  Although I'm having problems with the above script in that every time I submit the test it outputs the error "Sorry, this test has expired".  This must mean the script isn’t getting past the security check? I recently got a new host that uses a newer php version and this script worked fine before on the older php version. Can anybody spot any errors that would cause this script not to work on a newer php version?  I’m really stuck for ideas on this one.  Sorry for posting so much of the script.

 

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/37338-test-script-not-working/
Share on other sites

In an sql query above the script.

 

$query_entrytestdata = "SELECT * FROM entrypendingtest where id = '$testid'";
$entrytestdata = mysql_query($query_entrytestdata, $conn_abrv) or die(mysql_error());
$row_entrytestdata = mysql_fetch_assoc($entrytestdata);
$totalRows_entrytestdata = mysql_num_rows($entrytestdata);

$name = $row_entrytestdata['name'];

 

 

I think the error has something to do with the issues between the old and new php versions as this exact script was working on a older php version.

 

Thanks

 

 

I don't see anything in your code which has changed much recently.

 

Perhaps your last webhost had their PHP configured differently then this one does?

 

I think it has to do with the way you're comparing as well. Instead of using == for your string comparison, try using strcmp or stricmp. I've never really thought of == as a reliable method for string comparisons.

If the variables $testid and $useraction are coming from a form you need to explicitly reference them in the $_POST or $_GET supergloabal arrays, depending on the method used in the form. This is due to the changed of the default value for register_globals being changed from enabled to disabled.

 

Ken

I've used the superglobal arrays, Post and Get now and I'm now not getting any errors although none of the answers I submit in the test are being submitted.  How would I add post and get arrays to the following? 'user_answer[" . $i . "]' etc

 

print "<FORM ACTION='$PHP_SELF' METHOD='post'><BR>\n";
while ($j <= 20)  {
	print "Question #" . $j . ': ' . "<b>".$question[$i]."</b><BR>\n";
	print "<INPUT TYPE='radio' NAME='user_answer[" . $i . "]'  VALUE='A'>".$ans1[$i]."<BR>\n";
	print "<INPUT TYPE='radio' NAME='user_answer[" . $i . "]'  VALUE='B'>".$ans2[$i]."<BR>\n";
	print "<INPUT TYPE='radio' NAME='user_answer[" . $i . "]'  VALUE='C'>".$ans3[$i]."<P><P>\n";
	$i++;
	$j++;
}

//print "<INPUT TYPE='hidden' NAME='qkey' VALUE='$questionkey'>\n";	
$ii=0;
while ($ii<=19) {
	print "<input type='hidden' name='answer[$ii]' value='$answer[$ii]'>\n";
	print "<input type='hidden' name='orgid[$ii]' value='$orgid[$ii]'>\n";
	$ii++;

} 

Thanks very much.

The returned value would be in the $_POST array like this:

<?php
$_POST['user_answer'][$i]
?>

 

If you want to see what values are being returned to your script from the form, put these lines at the start of the processing script:

<?php
echo '<pre>$_GET: ' . print_r($_GET,true) . '</pre>';
echo '<pre>$_POST: ' . print_r($_POST,true) . '</pre>';
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.