Maz_2014 Posted July 4, 2014 Share Posted July 4, 2014 Could anyone help me please. I am trying to create a quiz in (quiz.php) which is works fine, but when I am trying to correct the quiz in another page (result.php) I had an error. I think the problem with posted answers and I couldn't solve it. This is my code in (quiz.php): <form method="post" action="result.php"> <?php session_start(); $questionNumber = 1; $query = mysql_query("SELECT * FROM Question WHERE TID = 'TEST' order by RAND()"); while ($result = mysql_fetch_array($query)) { ?> <?php echo $questionNumber . ") " . $result['Question'] . "<br>"; ?> <input type="radio" name="answer<?php echo $result["QID"] ?>" value="A"> <?php echo $result['A']; ?> <br> <input type="radio" name="answer<?php echo $result["QID"] ?>" value="B"> <?php echo $result['B']; ?> <br> <input type="radio" name="answer<?php echo $result["QID"] ?>" value="C"> <?php echo $result['C']; ?> <br> <input type="radio" name="answer<?php echo $result["QID"] ?>" value="D"> <?php echo $result['D']; ?> <br> <?php $questionNumber +=1; } ?> <input type="submit" name="checkQuiz" value="Check Quiz" onclick="location.href='result.php'" /> </form> And This is my code in (result.php): <?php if(isset($_POST['checkQuiz'])) { $correctAnswers = 0; $wrongAnswers = 0; $idList = join (',', array_map('intval', array_keys($_POST['answer']))); $sql = "SELECT QID, Answers FROM correct WHERE QID IN ($idList)"; while (list($QID, $correct) = mysql_fetch_row($res)) { if ($correct == $_POST['answer'][$QID]) { $correctAnswers +=1; } else { $wrongAnswers +=1; } } } ?> // in the body <?php $numberOfQs = $correctAnswers + $wrongAnswers; $score = round(($correctAnswers / $numberOfQs) * 100); ?> Correct Answers: <?php echo $correctAnswers; ?> <br> Wrong Answers: <?php echo $wrongAnswers; ?> <br> Score: <?php echo $score; ?> Quote Link to comment https://forums.phpfreaks.com/topic/289438-help-with-correcting-php-quiz/ Share on other sites More sharing options...
ginerjm Posted July 4, 2014 Share Posted July 4, 2014 And the error was? Quote Link to comment https://forums.phpfreaks.com/topic/289438-help-with-correcting-php-quiz/#findComment-1483839 Share on other sites More sharing options...
fastsol Posted July 4, 2014 Share Posted July 4, 2014 Well I can see a couple things right off the bat. Where are you defining $QID for this part? list($QID, $correct) And $res would be undefined since you haven't even ran the query, you only defined the query string with $sql. Quote Link to comment https://forums.phpfreaks.com/topic/289438-help-with-correcting-php-quiz/#findComment-1483840 Share on other sites More sharing options...
Maz_2014 Posted July 4, 2014 Author Share Posted July 4, 2014 Sorry. This is the error: " Warning: array_keys() expects parameter 1 to be array, null given in" Quote Link to comment https://forums.phpfreaks.com/topic/289438-help-with-correcting-php-quiz/#findComment-1483843 Share on other sites More sharing options...
Maz_2014 Posted July 4, 2014 Author Share Posted July 4, 2014 Dear fastsol, I think this is what you mean: <?php if(isset($_POST['checkQuiz'])) { $correctAnswers = 0; $wrongAnswers = 0; $idList = join (',', array_map('intval', array_keys($_POST['answer']))); $sql = "SELECT QID, Answers FROM correct WHERE QID IN ($idList)"; while($res = mysql_fetch_array( $sql )) { $correct = $res['Answers']; $QID = $res['QID']; if ($correct == $_POST['answer'][$QID]) { $correctAnswers +=1; } else { $wrongAnswers +=1; } } } ?> But still the same problem!! Quote Link to comment https://forums.phpfreaks.com/topic/289438-help-with-correcting-php-quiz/#findComment-1483846 Share on other sites More sharing options...
ginerjm Posted July 4, 2014 Share Posted July 4, 2014 That means read the manual and understand what that function requires. Quote Link to comment https://forums.phpfreaks.com/topic/289438-help-with-correcting-php-quiz/#findComment-1483847 Share on other sites More sharing options...
ginerjm Posted July 4, 2014 Share Posted July 4, 2014 BTW - why do you have an onclick to direct you to the result.php script as well as a form directing the submission to the same script? The onclick is not needed, and depending on whether it happens before the actual form submission, it may cause you problems in result.php. Quote Link to comment https://forums.phpfreaks.com/topic/289438-help-with-correcting-php-quiz/#findComment-1483848 Share on other sites More sharing options...
Maz_2014 Posted July 4, 2014 Author Share Posted July 4, 2014 ginerjm I read the manual and the problem is whit this line: $idList = join (',', array_map('intval', array_keys($_POST['answer']))); and I removed the onclick direct, but still the problem! Quote Link to comment https://forums.phpfreaks.com/topic/289438-help-with-correcting-php-quiz/#findComment-1483851 Share on other sites More sharing options...
ginerjm Posted July 4, 2014 Share Posted July 4, 2014 When you have assured yourself that you are using the proper syntax then the problem must lie with your data. If you had validated your input before trying to use it you would have caught this error. There is NO element $_POST['anwer']. You do have an element in the POST array named "answer?" where ? is the value of your QID column. Quote Link to comment https://forums.phpfreaks.com/topic/289438-help-with-correcting-php-quiz/#findComment-1483853 Share on other sites More sharing options...
Maz_2014 Posted July 4, 2014 Author Share Posted July 4, 2014 ginerjm Thank you. But could you please tell me how can I use the $_POST['answer?'] when it is in <input type="radio" name="answer<?php echo $result["QID"] ?>" because I tried several ways but with out success!! Quote Link to comment https://forums.phpfreaks.com/topic/289438-help-with-correcting-php-quiz/#findComment-1483862 Share on other sites More sharing options...
ginerjm Posted July 4, 2014 Share Posted July 4, 2014 Your code is not what you think it is. Your name attribute is the string "answer" followed immediately by the value of your QID column from your query. So if QID for that row is "123" then your name is "answer123". So to extract it form the post array you would have to use "$_POST['answer123']. But all of this is meaningless since the whole point of radio buttons is to provide a set of choices from which the user may only choose ONE option. This will not produce an array in $_POST, but merely a single value so your very complicated code to manage the returned value from the user is unnecessary. Quote Link to comment https://forums.phpfreaks.com/topic/289438-help-with-correcting-php-quiz/#findComment-1483863 Share on other sites More sharing options...
Maz_2014 Posted July 4, 2014 Author Share Posted July 4, 2014 (edited) Thank you ginerjm I got your point. But any suggestions for a simple code to get what I planning to do? Edited July 4, 2014 by Maz_2014 Quote Link to comment https://forums.phpfreaks.com/topic/289438-help-with-correcting-php-quiz/#findComment-1483865 Share on other sites More sharing options...
ginerjm Posted July 4, 2014 Share Posted July 4, 2014 (edited) You need to assemble your quiz form so that the answers relate to a question number. Your attempt so far is completely wrong since you don't do that and since you are trying to gather the correct answers using the values of the selected answers from the answer table, rather than the question #. If you are going to write code, learn how to (try to) separate your html from your php as much as possible. Example: You begin your first piece of code with an html form tag, and then jump into php mode. Silly. Instead - do your logic, process your input, get your data, THEN build your output Edited July 4, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/289438-help-with-correcting-php-quiz/#findComment-1483867 Share on other sites More sharing options...
fastsol Posted July 5, 2014 Share Posted July 5, 2014 Dear fastsol, I think this is what you mean: <?php if(isset($_POST['checkQuiz'])) { $correctAnswers = 0; $wrongAnswers = 0; $idList = join (',', array_map('intval', array_keys($_POST['answer']))); $sql = "SELECT QID, Answers FROM correct WHERE QID IN ($idList)"; while($res = mysql_fetch_array( $sql )) { $correct = $res['Answers']; $QID = $res['QID']; if ($correct == $_POST['answer'][$QID]) { $correctAnswers +=1; } else { $wrongAnswers +=1; } } } ?> But still the same problem!! You're still not running the query!! You need to call mysql_query() before the while loop. Quote Link to comment https://forums.phpfreaks.com/topic/289438-help-with-correcting-php-quiz/#findComment-1483930 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.