Jump to content

akramm

New Members
  • Posts

    3
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

akramm's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Hi Guys...... I would appreciate ur help on this.. I have the following code which scores a quiz after it has been submitted. The script works but it doesnt insert the information into the databse. I tested the query in MySQL front and it does work, so its something wrong with my PHP script. Ive tried moving the query around but it does not work. <?php session_start(); // This script handle all the user commands. require_once ("database.php"); require_once ("globals.php"); require_once ("loginlib.php"); $usertype = Getusertype (); $username = Getusername (); $userid = Getuserid (); $title = $g_system_name . " -- Dashboard"; ShowHeader ($title); ShowLinks (5); ?> <? //require_once ('mysql_connect.php'); // Connect to the db. mysql_connect("localhost", "root", "") or die ("Unable to connect to the server"); // select the database or return an error message mysql_select_db("nluser") or die ("Unable to connect to the database"); $total = $_POST['total']; $quizID = $_SESSION['id']; $noCorrect = 0; echo "<b>Quiz ID: </b>".$quizID; $result = mysql_query("SELECT * FROM questions WHERE quizID='$quizID'"); if(mysql_numrows($result)) { while($rows = mysql_fetch_row($result)) { //$rows = mysql_fetch_row($result); //print_r($row); //echo "start of the while loop - under the form<br>"; $qid=$rows[0]; $question=$rows[1]; $question_no=$rows[2]; $rightanswer=$rows[3]; $selection = "answer".$question_no; //echo "assign values to array<br>"; $selectedAnswer = $_POST[$selection]; echo "<br><br><b>".$question_no."</b>) ".$question."<br/>"; if($selectedAnswer == $rightanswer) { echo "<br><b>Correct: </b>".$rightanswer; $noCorrect++; } else { echo "<br><b>Incorrect: </b>".$selectedAnswer; } } print("<br><br><b>Total correct: </b>".$noCorrect." out of ".$total); $query = "INSERT INTO score (scoreID, username, score, quizID) VALUES (null, '$username', '$noCorrect', '$quizID')"; } //echo $query; ?> Could you help me get this to work!? Many Thanks! mod edit : use ... tags around your code
  2. Thanks Alot.. It worked... but I can only select one answer for all questions..... how do I get round that? Thanks!!
  3. Hi, i am having problems with the following code for a quiz. When the code is executed it prints out all the questions from the database along with the four answers related to it. Originally the form was outisde of the while loop but this would only let me select one radio button on the whole page. When i put the form into the while loop it allowed me to select an answer for each question however this resulted in multiple submit buttons. Is there a way i can retrieve all questions from the database related to that quiz on one page? Line number On/Off | Expand/Contract <?php session_start(); // This script handle all the user commands. require_once ("database.php"); require_once ("globals.php"); require_once ("loginlib.php"); $usertype = Getusertype (); $username = Getusername (); $userid = Getuserid (); $title = $g_system_name . " -- Dashboard"; ShowHeader ($title); ShowLinks (5); ?> <? //require_once ('mysql_connect.php'); // Connect to the db. mysql_connect("localhost", "root", "") or die ("Unable to connect to the server"); // select the database or return an error message mysql_select_db("nluser") or die ("Unable to connect to the database"); //get the id if the quiz that is to taken $id = $_GET['id']; echo "Quiz ID: ".$id; ?> <br><br> <? //$query = "SELECT * FROM questions WHERE quizID='$id' "; //$result=mysql_query($query); //echo mysql_query($query); $result = mysql_query("SELECT * FROM questions WHERE quizID='$id'"); //while($row = mysql_fetch_row($result)) //{ //echo $row['question_no'].",".$row['question']."<br/>"; //sergeys //echo "query works<br>"; $answers = array(); $question=""; $answerblock=array(); $rightanswer=array(); //echo "arrays work<br>"; //{ //print_r($row); if(mysql_numrows($result)) { while($rows = mysql_fetch_row($result)) { print('<form name=myform method="post" action="quizresults.php">'); //$rows = mysql_fetch_row($result); //print_r($row); //echo "start of the while loop - under the form<br>"; $qid=$rows[0]; $question=$rows[1]; $answers[0]=$rows[3]; $answers[1]=$rows[4]; $answers[2]=$rows[5]; $answers[3]=$rows[6]; $question_no=$rows[2]; $rightanswer=$rows[3]; //echo "assign values to array<br>"; srand((float) microtime() * 10000000); $rand_keys = array_rand($answers, 4); $answer0=$answers[$rand_keys[0]] ; $answer1=$answers[$rand_keys[1]] ; $answer2=$answers[$rand_keys[2]] ; $answer3=$answers[$rand_keys[3]] ; print('<tr><td><input type="radio" name=answer value="'.$answer0 .'"> '.$answer0.'</td></tr>'); print('<tr><td><input type="radio" name=answer value="'.$answer1 .'">&nbsp&nbsp&nbsp'.$answer1 .'</td></tr>'); print('<tr><td><input type="radio" name=answer value="'.$answer2 .'">&nbsp&nbsp&nbsp'.$answer2 .'</td></tr>'); print('<tr><td><input type="radio" name=answer value="'.$answer3 .'">&nbsp&nbsp&nbsp'.$answer3 .'</td></tr><br>'); print('<tr><td>&nbsp<input type=hidden name=rightanswer value="'.$rightanswer .'"></tr>'); print('<tr><td colspan=2><input type=submit name=submit value="Submit" > &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp <input type=reset name=reset value="reset" ></td></tr></table>'); print('</form>'); } //print('<tr><td>&nbsp<input type=hidden name=rightanswer value="'.$rightanswer .'"><input type= hidden name=qnumber value="'.$qnumber .'"></tr>'); } ?> </form> <? ShowFooter(); ?>
×
×
  • 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.