Dynamic Multiple Input Processing

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am creating a simple "or so I thought" php/mysql survey.
There are 3 types of Surveys, with different questions. 1 for Clients,
1 for Employees, 1 for Peers.
Questions for each survey are created, editable, removable, and drag/
drop sortable.
Options to these questions are dynamic themselves.

Basically, everything works except when I try to Insert the data into
the answers table.

The one question I'm looking for is how would one process the data
based on the respective question_id's?
Sending it to another page would require an excessive amount of _POST
variables, and like i said before, there are different questions for
each survey.

or would this work? (it doesn't seem to process)
for($i=$start; $i<=$end; $i++){
      $query[$i] = "INSERT INTO answer(ansresult, ansdesc, idquestion,
idsurvey) VALUES('q_$i','exp_$i','$idquestion','$surveyType')";
      $result[$i]= mysql_query($query[$i]);

$msg = sprintf("Success: %d, Questions: %d",$success,$end-$start);

Any help, advice, or direction would be greatly appreciative.
- Justin Michaliga

Re: Dynamic Multiple Input Processing

That will work.  Instead of 'q_$i', you can use $_POST["q_$i"].

Here's an example:

# survey.php

  $items = array();
  foreach ($_POST['answers'] as $id => $answer) {
    $items[] = array($id, $answer);
  $db = MDB2::singleton('mysql://u:p@h/d');
  $st = $db->prepare('insert into answer(questionRef, answer)
    values (?, ?)');
  $count = $db->extended->executeMultiple($st, $items);
  echo "Inserted $count answers.<br>";


<form action=survey.php method=post>
  <input name="answers[1]"><br>
  <input name="answers[2]"><br>
  <input name="answers[3]"><br><br>
  <input type=submit>
  <input type=hidden name=submit value=1>

Quoted text here. Click to load it

Site Timeline