Hi,
I am currently developing an application for running online surveys. I have some groundwork done and so far have coded some of the admin area. Currently you can create a survey, add questions, add possible answers to each question and control what way it is displayed (ie: radio button, checkbox or select menu.)
I have started work on a survey display page and i use some while statements to display the questions. This is working fine but, where I run into problems is when i want to enter the responses into teh DB.
Here's some sample code for displaying a question:
$qid = $qrow['qid'];
$q1 = "SELECT * FROM answers WHERE sid = '$sid' AND qid = '$qid'";
$r1 = mysql_query($q1) or die(mysql_error());
echo "<br>";
while($q1r = mysql_fetch_array($r1)){
echo $q1r['answer']." - <input type=\"radio\" name=\"".$qid."\"
value=\"".$q1r['aid']."\"><br>\n";
}//end radio button loop
echo "<p>\n";
This outputs a radio button type question. Say, I have 3 questions, each of type radio button. The name of each radio button will be set to the $qid which is the ID for the question as stored in the DB. The value of each different radio button will be set to $q1r['aid'] which is the answer ID as stored in the DB. The problem I have is, how can I insert the answers into a DB?
The database table where the answers will go is like this:
table:response(rid,qid,sid,response) In an insert statement I would insert $qid into qid, $sid into sid and the value of the selected radio button into 'response'. qid is the question ID and sid is the survey ID. Now, a standard insert for this is simple but the problem i have is because the number of questions is dynamic and the $qid is the name of the variable and the response is the value of that variable. How can I make it so that my insert code will loop through the passed variables and insert the variable name as the 'qid' and the variable value as the 'response'.
Hopefully I explained that clearly enough! Any help (or alternative ideas) is appreciated.
Thanks in advance,
Martin