I have created a form that allows users to make a multiple choice quiz. The quiz can have up to 9999 questions and 99 options for each question (should they really need it), then store that quiz in a database.
My problem is that I cannot work out how to put the user created quiz in to the database. I know the mysql INSERT command, but cannot work out (or find) how to determine how many form inputs are being passed from one page to the next.
I would normally do:
$name = $_POST['name'];
$choices = $_POST['choices'];
$quizData = "INSERT INTO quiz
(name, choices)
VALUES
('$name',
'$choices')"
But how do I do this without knowing how many questions and choices there are?
Since $_POST is an array, use foreach to set each question or answer into the database or into it’s own variable. If your fields are called questions, set them up like
<input type='text' name='questions[]', you can then count how many questions there are in the $_POST[‘questions’] or whatever you call your questions fields array using count().