I am trying to set up a survey system. The survey questions themselves come from a database.

I have created the page that pulls the questions from the mysql table into an html form but can't work out how to insert the responses into the database. I think i will need multiple rows inserted, one for each question. Each row will just need two fields: a key that remains the same for all rows added in that session and the value of the response. The value of the response will be the id of the answer as saved in the "answers" table.

The form code i have is:

<form action="submitquestionnaire.php" method="POST" name="questionnaire" id="questionnaire">
<input name="s_id" type="hidden" value="<?php echo $_GET['s']; ?>" />
<ul id="questions">
  <?php do { 
  $qid = $row_questions['q_id'];
  mysql_select_db($database_conn, $conn);
$query_answers = "SELECT * FROM answers WHERE answers.id_question=$qid ORDER BY answers.a_number";
$answers = mysql_query($query_answers, $conn) or die(mysql_error());
$row_answers = mysql_fetch_assoc($answers);
$totalRows_answers = mysql_num_rows($answers);
  <li class="question question-<?php echo $row_questions['q_number']; ?>">
      <div class="q-text"><?php echo $row_questions['q_text']; ?></div>
      <div class="q-description description"><?php echo $row_questions['q_description']; ?></div>
      <div class="answer">
        <?php if ( $row_questions['q_type'] == 0 ) { ?>
        <input name="answer" type="text" />
        <?php } elseif($row_questions['q_type'] == 1 ){ ?>
        <?php do { ?>
            <input type="radio" name="answer" value="<?php echo $row_answers['a_id']; ?>" id="<?php echo $row_answers['a_id']; ?>" />
            <?php echo $row_answers['a_text']; ?></label>
          <div class="a-description description"><?php echo $row_answers['a_description']; ?></div>
          <?php } while ($row_answers = mysql_fetch_assoc($answers)); ?>
<?php } elseif($row_questions['q_type'] == 2 ){ ?>
        <?php do { ?>
            <input type="checkbox" name="answer" value="<?php echo $row_answers['a_id']; ?>" id="<?php echo $row_answers['a_id']; ?>" />
            <?php echo $row_answers['a_text']; ?></label>
          <div class="a-description description"><?php echo $row_answers['a_description']; ?></div>
         <?php } while ($row_answers = mysql_fetch_assoc($answers)); ?>
        <?php } ?></div>
    <?php } while ($row_questions = mysql_fetch_assoc($questions)); ?>
<input type="submit" />
<br /><br />

As you can see the answer can be one of three types: text, radio or checkbox. This page displays fine, apart from all the radio buttons are seen as being in the same group because the radio name parameter is the same for them all. I set it to be the same because on the php page i have:

<?php require_once('Connections/conn.php'); 

//initialize the session
if (!isset($_SESSION)) {
$response_id = mysql_query('SELECT LAST_INSERT_ID()');

foreach($_POST['questionnaire'] as $index => $val){
$sqlquery = "INSERT INTO response_answers VALUES('r_id', ra_id')"; 
$results = mysql_query($sqlquery);

$response_id = mysql_query('SELECT LAST_INSERT_ID()');

    if (count($result > 0))
        $new = array();
        foreach ($result as $key => $value)
            $new[] = "('" . $value["answer"] . "', '" . $response_id . "')";
        if (count($new) > 0)
        $query = mysql_query("INSERT INTO response_answers (ra_id, r_id) VALUES " . implode(', ', $new)");
            if ($query)
                echo "SUCCESS";
                echo "FAILED";

Which i was hoping would create an array for each question and then enter each of the arrays into the response_answers table on a separate row. This is why the radio name parameter has to be the same, so the php code knows what to pull from.

You will also see in there a SELECT LAST_INSERT_ID, this is as the previous page to this form page creates the questionnaire response value in a separate response_questionnaire table.

Does anyone know how i can do this? I hope it makes sense, I am having some difficulty explaining it.


6 Years
Discussion Span
Last Post by dany12

So if I undertstand you correctly you have a survey that has multiple choices and the choices are radio buttons checkboxes and you want to post them in your php and then create an array and insert them in a sql database
I think you should look in php manual how to create a variable that stores your array and then insert that variable in the database.
Another solution I think is to create an array an use

foreach($yourvariable as row=>value){

//insert the values one by one


I don't know if this is what your are looking for.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.