topflight Posted September 5, 2009 Share Posted September 5, 2009 Hello I have created like my own exam system, and I was testing it put and when I put everything correct my score still comes out to 90 instead of 100 please help. exam.php <?php $elist = mysql_query("SELECT * FROM `entrance_exam` ORDER BY rand()"); $elrows = mysql_num_rows($elist); if ($elrows > '0'){ ?> <table width="100%"> <?php while ($elr = mysql_fetch_array($elist)){ $counter = $counter + 1; ?> <form action="?page=correct_exam" method="post" name="counter"> <input type="hidden" size="8" name="timer"> <script> <!-- // var milisec=0 var seconds=30 document.counter.timer.value='30' function display(){ if (milisec<=0){ milisec=9 seconds-=1 } if (seconds<=-1){ milisec=0 seconds+=1 } else milisec-=1 document.counter.d2.value=seconds+"."+milisec setTimeout("display()",100) } display() --> </script> <?php echo "<tr bgcolor=#4c7cb2><td><font color=white><b>$counter. $elr[Question]</b></font></tr>"; echo " <tr bgcolor=#EEEEEE><td>A:<input type=radio name=$elr[id] value=a> $elr[A]</td></tr>"; echo "<tr bgcolor=#EEEEEE><td>B:<input type=radio name=$elr[id] value=b> $elr[b]</td></tr>"; if ($elr[C]){ echo " <tr bgcolor=#EEEEEE><td>C:<input type=radio name=$elr[id] value=c> $elr[C]</td></tr>"; } if ($elr[D]){ echo " <tr bgcolor=#EEEEEE><td>D:<input type=radio name=$elr[id] value=d> $elr[D]</td></tr>"; } } echo "<tr><td><input type=submit value=\"Submit Exam\"></td></tr>"; echo "<input type=hidden name=now value=$now>"; echo "<input type=hidden name=viewexam value=yes>"; } else { echo'No Questions in database'; } ?> </table></form> correct_exam.php <?php include("jbusecure/dbconfig09.php"); $a1 = $_POST[1]; $a2 = $_POST[2]; $a3 = $_POST[3]; $a4 = $_POST[4]; $a5 = $_POST[5]; $a6 = $_POST[6]; $a7 = $_POST[7]; $a8 = $_POST[8]; $a9 = $_POST[9]; $a10 = $_POST[10]; $a11 = $_POST[11]; $a12 = $_POST[12]; $a13 = $_POST[13]; $a14 = $_POST[14]; $a15 = $_POST[15]; $a16 = $_POST[16]; $a17 = $_POST[17]; $a18 = $_POST[18]; $a19 = $_POST[19]; $a20 = $_POST[20]; $correct = 0; # Initialize the Variable if ($a1 == 'a'){ $correct = $correct + 1; } if ($a2 == 'a'){ $correct = $correct + 1; } if ($a3 == 'a'){ $correct = $correct + 1; } if ($a4 == 'a'){ $correct = $correct + 1; } if ($a5 == 'a'){ $correct = $correct + 1; } if ($a6 == 'a'){ $correct = $correct + 1; } if ($a7 == 'a'){ $correct = $correct + 1; } if ($a8 == 'a'){ $correct = $correct + 1; } if ($a9 == 'a'){ $correct = $correct + 1; } if ($a10 == 'a'){ $correct = $correct + 1; } if ($a11 == 'a'){ $correct = $correct + 1; } if ($a12 == 'a'){ $correct = $correct + 1; } if ($a13 == 'a'){ $correct = $correct + 1; } if ($a14 == 'a'){ $correct = $correct + 1; } if ($a15 == 'a'){ $correct = $correct + 1; } if ($a16 == 'a'){ $correct = $correct + 1; } if ($a17 == 'a'){ $correct = $correct + 1; } if ($a18 == 'a'){ $correct = $correct + 1; } if ($a19 == 'a'){ $correct = $correct + 1; } if ($a20 == 'a'){ $correct = $correct + 1; } $score = round((($correct / 20) * 100),0); echo "Your score was $score"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/173189-solved-not-adding-correctly/ Share on other sites More sharing options...
sKunKbad Posted September 5, 2009 Share Posted September 5, 2009 Is there a $_POST[0] ?? Quote Link to comment https://forums.phpfreaks.com/topic/173189-solved-not-adding-correctly/#findComment-912933 Share on other sites More sharing options...
topflight Posted September 5, 2009 Author Share Posted September 5, 2009 no Quote Link to comment https://forums.phpfreaks.com/topic/173189-solved-not-adding-correctly/#findComment-912935 Share on other sites More sharing options...
sKunKbad Posted September 5, 2009 Share Posted September 5, 2009 what does var_dump($_POST) output? Quote Link to comment https://forums.phpfreaks.com/topic/173189-solved-not-adding-correctly/#findComment-912936 Share on other sites More sharing options...
topflight Posted September 5, 2009 Author Share Posted September 5, 2009 array(23) { ["timer"]=> string(0) "" [19]=> string(1) "a" [13]=> string(1) "a" [5]=> string(1) "a" [4]=> string(1) "a" [10]=> string(1) "a" [14]=> string(1) "a" [21]=> string(1) "a" [12]=> string(1) "a" [22]=> string(1) "a" [20]=> string(1) "a" [18]=> string(1) "a" [7]=> string(1) "a" [16]=> string(1) "a" [8]=> string(1) "a" [11]=> string(1) "a" [2]=> string(1) "a" [15]=> string(1) "a" [6]=> string(1) "a" [17]=> string(1) "a" [3]=> string(1) "a" ["now"]=> string(0) "" ["viewexam"]=> string(3) "yes" } Your score was 90 Quote Link to comment https://forums.phpfreaks.com/topic/173189-solved-not-adding-correctly/#findComment-912937 Share on other sites More sharing options...
sKunKbad Posted September 5, 2009 Share Posted September 5, 2009 In correct_exam.php, you are checking for the value of $_POST[1], but there is no key [1] or [9] in your var_dump: array(23) { ["timer"] => string(0) "" [19] => string(1) "a" [13] => string(1) "a" [5] => string(1) "a" [4] => string(1) "a" [10] => string(1) "a" [14] => string(1) "a" [21] => string(1) "a" [12] => string(1) "a" [22] => string(1) "a" [20] => string(1) "a" [18] => string(1) "a" [7] => string(1) "a" [16] => string(1) "a" [8] => string(1) "a" [11] => string(1) "a" [2] => string(1) "a" [15] => string(1) "a" [6] => string(1) "a" [17] => string(1) "a" [3] => string(1) "a" ["now"] => string(0) "" ["viewexam"]=> string(3) "yes" } Your score was 90 You also have a key [21] and [22]. Did you notice? Quote Link to comment https://forums.phpfreaks.com/topic/173189-solved-not-adding-correctly/#findComment-912941 Share on other sites More sharing options...
topflight Posted September 5, 2009 Author Share Posted September 5, 2009 I am confused what happen please Quote Link to comment https://forums.phpfreaks.com/topic/173189-solved-not-adding-correctly/#findComment-912942 Share on other sites More sharing options...
sKunKbad Posted September 5, 2009 Share Posted September 5, 2009 If you look at the result of your database query, you must have skipped 1 and 9, and there is now a 21 and 22. Also, your opening form tag needs to be above your while loop, and the closing form tag needs to be before your closing table tag. Quote Link to comment https://forums.phpfreaks.com/topic/173189-solved-not-adding-correctly/#findComment-912945 Share on other sites More sharing options...
topflight Posted September 5, 2009 Author Share Posted September 5, 2009 ok I change the ID number in the database and now I am getting the following.... array(23) { ["timer"]=> string(0) "" [17]=> string(1) "a" [10]=> string(1) "a" [5]=> string(1) "a" [9]=> string(1) "a" [19]=> string(1) "a" [20]=> string(1) "a" [15]=> string(1) "a" [6]=> string(1) "a" [13]=> string(1) "a" [21]=> string(1) "a" [1]=> string(1) "a" [4]=> string(1) "a" [7]=> string(1) "a" [18]=> string(1) "a" [3]=> string(1) "a" [12]=> string(1) "a" [11]=> string(1) "a" [16]=> string(1) "a" [2]=> string(1) "a" [14]=> string(1) "a" ["now"]=> string(0) "" ["viewexam"]=> string(3) "yes" } Your score was 95 Their is 21 questions in the database, but I have it get a random 20. so do you think that can might be the problem. Quote Link to comment https://forums.phpfreaks.com/topic/173189-solved-not-adding-correctly/#findComment-912947 Share on other sites More sharing options...
sKunKbad Posted September 5, 2009 Share Posted September 5, 2009 Yes, this is probably where your problem comes from. I just want to add, that this exam system would be fundamentally flawed, because if the user simply views the source code, they would have all of the correct answers. If it matters, it should be changed. When I have created an exam in the past, the questions, answers, and scores were held in an XML file, and then parsed with SimpleXML for question and answers retrieval, and also for scoring. This makes changing the questions, answers, and scoring easy too. An instructor can just create a new XML file, and the new test is ready to go. Quote Link to comment https://forums.phpfreaks.com/topic/173189-solved-not-adding-correctly/#findComment-912949 Share on other sites More sharing options...
topflight Posted September 5, 2009 Author Share Posted September 5, 2009 Well the user cant find the correct answer becuase I have it set up like the following: if($a1 == 'b'){ $correct == $correct + 1} then it adds one point, I have it like that for every question but my question is that how come when I made all the right answer to be "A" and then I select A on all the answers my result comes out ta 95? Quote Link to comment https://forums.phpfreaks.com/topic/173189-solved-not-adding-correctly/#findComment-912953 Share on other sites More sharing options...
sKunKbad Posted September 5, 2009 Share Posted September 5, 2009 OK.... I just don't know. It's hard to diagnose because there are quite a few things that look wrong. I think you should run your query directly inside MySQL or phpMyAdmin, and see if you are getting what you expect. Also, until you validate the output of your exam, your form itself could be messing up what you think is perfect. Maybe somebody else will chime in. Quote Link to comment https://forums.phpfreaks.com/topic/173189-solved-not-adding-correctly/#findComment-912954 Share on other sites More sharing options...
topflight Posted September 5, 2009 Author Share Posted September 5, 2009 their we go now it works thanks I rand it through phpmyadmin thanks. Quote Link to comment https://forums.phpfreaks.com/topic/173189-solved-not-adding-correctly/#findComment-912955 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.