Points per correct answer using php and mysql


I am trying to figure out how to create a system where the user answers a bunch of questions once a week and gets a point per right answer. The admin will add the correct answers the week after so the users answers must be stored in the database.

Would appriciate help how to accomplice this using php and mysql. Thanks!

You'll need a few pages:

1) Questions page:

In the questions.php page you could have a form under each question that would allow for the user to select checkboxes:

<form action="answers_submitted.php" method="POST">
What is 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">

2) Answers Submitted page:

The answers_submitted.php page would have to retrieve these values once the user clicks the "Send Answers". After retrieving the answers, those answers would be compared with the right answers in order to get a score. For example, if there were 4 questions in total:


//array containing the right answers
$array_of_answers = array(6,2,"food", 33, "books");

$score = 0;//this value will contain the score once all the code
           //has been executed.

for ($i = 0; $i<4; $i++)
  $string = 'answer'.$i;
    if ($_POST[$string] == $array_of_answers[$i])


Then that score could be saved to a database server, like MySQL for instance and then displayed to the users the following week. This code could be done on page answers_submitted.php as well. Here is a simplified example of how to send the information to the database, but I do recommend reading a complete tutorial on the matter:

$sql = "UPDATE users SET score = {$score} WHERE id = {$_SESSION['user_id']}";
$stmt = $conn->query($sql);

Since the $score value was not a direct user input, and since the $_SESSION['user_id'] value was generated on the server, prepared statements and binding the parameters were not needed in this case, THOUGH in most cases these are needed to prevent sql injection. Some coders even apply those methods in all cases (even when not needed) just to be in the habbit of using them, so keep that in mind when making your own code!