Points per right answer using php and mysql -


i trying figure out how create system user answers bunch of questions once week , gets point per right answer. admin add correct answers week after users answers must stored in database.

would appriciate how accomplice using php , mysql. thanks!

you'll need few pages:

1) questions page:

in questions.php page have form under each question allow user select checkboxes:

<form action="answers_submitted.php" method="post"> 1 + 5 equal to?<br>   <input type="radio" name="answer0" value="3"> 3    <input type="radio" name="answer0" value="6"> 6    <input type="radio" name="answer0" value="99"> 99 <br>   <input type="submit" value="send answers"> </form> 

2) answers submitted page:

the answers_submitted.php page have retrieve these values once user clicks "send answers". after retrieving answers, answers compared right answers in order score. example, if there 4 questions in total:

<?php  //array containing right answers $array_of_answers = array(6,2,"food", 33, "books");  $score = 0;//this value contain score once code            //has been executed.  ($i = 0; $i<4; $i++) {   $string = 'answer'.$i;   if(isset($_post[$string]))   {     if ($_post[$string] == $array_of_answers[$i])     $score++;   } }  ?> 

then score saved database server, mysql instance , displayed users following week. code done on page answers_submitted.php well. here simplified example of how send information database, recommend reading complete tutorial on matter:

$sql = "update users set score = {$score} id = {$_session['user_id']}"; $stmt = $conn->query($sql); 

since $score value not direct user input, , since $_session['user_id'] value generated on server, prepared statements , binding parameters not needed in case, though in cases these needed prevent sql injection. coders apply methods in cases (even when not needed) in habbit of using them, keep in mind when making own code!


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -