Click here to Skip to main content
15,039,359 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I'm coding an exam app for which questions and their corresponding answers (MCQ model) have to be registered at once via a form.

What I wish to do is collect from the same form a question, the corresponding answer options and mark/check which of the answer options is the correct response to the question before inserting all of this data to the database, in such a way that I could easily set the correct answer option to 1 and the rest to 0 when saving to the database.

I'm somehow confused on how to google this.
Any reference, link, sample, tutorial will greatly help me, please!
The app is in PHP using MySQL as DBMS.

What I have tried:

I already succeed in saving the answer options with the corresponding question to the DB. I'm now looking for a way to check the correct answer before saving.
Updated 20-Jan-20 11:34am

1 solution

There should be at two tables for the basics; one table for questions and the other for the answers.
This is the over-simplified version; not in any particular flavor of SQL, and there are no keys or indexes installed (but recommended)
CREATE TABLE mcqQuestions (
  QuestionID INT,       -- should be Primary Key, auto populate, etc
  QuestiontEXT NVARCHAR(100)

CREATE TABLE mcqAnswers (
  AnswerID INT,      -- should be Primary Key, auto populate, etc
  QuestionID INT,    -- should be verified that Question exists
  AnswerCorrect BIT, -- only one per question
  AnswerText NVARCHAR(100)
Now on the HTML side; the form I would use would have the question an answers, along with a radio button for marking which answer is correct. If you make sure they are on the same form and all have the same name, only one of them can be checked at any give time. The differentiation is done be the assigned value
<form action="" method="post">
	<input type="text" name="Question" />
	<input type="radio" name="CorrectAnswer" value="1" />
	<input type="text" name="Answer1" />
	<input type="radio" name="CorrectAnswer" value="2" />
	<input type="text" name="Answer2" />

	<input type="radio" name="CorrectAnswer" value="3" />
	<input type="text" name="Answer3" />

	<input type="radio" name="CorrectAnswer" value="4" />
	<input type="text" name="Answer4" />

	<input type="submit" />
Pressing the submit button on this will POST the following data:
Question: {text from input}
Answer1: {text from input}
Answer2: {text from input}
Answer3: {text from input}
Answer4: {text from input}
CorrectAnswer: {one of the four values assigned in the source code}
Sorry but I do not write PHP... but the logic is relatively simple:

1. Read the question
2. Save the question to the DB and get it's ID

3. Read the CorrectAnswer value.
4. Read all 4 answers

Save each answer individually, using the respective input element and questionID (from #2), AND if it the correct answer save that as well

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900