All,
I’m allowing my user to create questions which I’ll need to save answers to. They can create as little or as many questions as they would like. My dilemma is how to save these variables and also retrieve and display them back to the user.
When I output the form to the user I have the following PHP code (I have the questions stored in a different table as well as the answers that are applicable in a different table):
$qryquestion = "Select * from feedback_questions order by question_display ASC";
$resultquestion = mysql_query($qryquestion);
$i=1;
while($resultsetquestion = mysql_fetch_array($resultquestion)){
if ($i%2==0){
echo '<tr bgcolor="#FFFFFF" id="row'.$i.'">';
} else {
echo '<tr id="row'.$i.'">';
}
echo '<td align="center"><b>'.$resultsetquestion['question_value'].'</b></td>';
echo '<td align="center"><div style="text-align:center;"><input type="radio" name="rating_value_'.$resultsetquestion['question_id'].'" id="rating_value_'.$resultsetquestion['question_id'].'" value="'.$resultsetratings['rating_id'].'"></td>';
}
I was thinking about saving them in the db like the following:
user_id question_id answer_id feedback_id 1 1 5 1 1 2 5 1 1 3 1 1 2 1 2 2
How would I save these variables from a $_POST perspective when the form gets submitted?
Since you’re using PHP, the easiest method to handle arbitrary numbers of questions will be to convert your inputs into a PHP array once POSTed, as specified here. An example with a PHP implementation can be found here. Once you actually have the input data in a PHP array, you can just add them to the database. Your database layout looks appropriate enough, though you could probably get away with actually including the question and answer in the same table, to save on joins.