I’m trying to process a 8 question survey where each question is not mandatory. So I never know which fields are filled out until i’m processing them. The questions are radio buttons with either a “yes” or “no” value.
I was thinking about creating a query string for both the fields and values and then insert those values into a insert string. I can’t seem to figure out how to know things like which will be the last fields with a value so I don’t put in another comma.
Any ideas on how best to process this?
Here is what I have so far
$fields = "";
$values = "";
if(!empty($_POST['q1']))
{
$fields .= "a1";
$values .= " '".$_POST['q1']."' ";
}
if(!empty($_POST['q2']))
{
$fields .= "a2";
$values .= " '".$_POST['q2']."' ";
}
...
...
...
$survey_query = "INSERT INTO survey (".$fields.")VALUES(".$values.")";
// this is what the insert statement should look like if all fields were filled out
// INSERT INTO survey (voter_id, a1, a2, a3, a4, a5, a6, a7, a8)
//VALUES ("255.255.255.255", "yes", "yes", "no", "yes", "yes", "yes", "no", "yes");