1

I'm trying currently to insert back into a database values from my form.

 <?php do { ?>
  <tr>
    <td><?php echo $row_questions['question']; ?><br /><table><tr><td style=

    "padding:15px; text-align:center">
      <label>
        <center><input type="radio" name="answers_<?php echo $row_questions['id']; ?>_<?php echo $row_questions['sub_category']; ?>" value="1" id="answers_1" /></center><br />
        Low</label>
      </td><td style="padding:15px; text-align:center">
      <label>
        <center><input type="radio" name="answers_<?php echo $row_questions['id']; ?>_<?php echo $row_questions['sub_category']; ?>" value="2" id="answers_2" /></center><br />
        Fairly Low</label>
      </td><td style="padding:15px; text-align:center">
      <label>
        <center><input type="radio" name="answers_<?php echo $row_questions['id']; ?>_<?php echo $row_questions['sub_category']; ?>" value="3" id="answers_3" /></center><br />
        Average</label>
      </td><td style="padding:15px; text-align:center">
      <label>
        <center><input type="radio" name="answers_<?php echo $row_questions['id']; ?>_<?php echo $row_questions['sub_category']; ?>" value="4" id="answers_4" /></center><br />
        Fairly High </label>
      </td><td style="padding:15px; text-align:center">
      <label>
        <center><input type="radio" name="answers_<?php echo $row_questions['id']; ?>_<?php echo $row_questions['sub_category']; ?>" value="5" id="answers_5" /></center><br />
        High</label>
      </td></tr></table><br />
  </tr>
  <?php } while ($row_questions = mysql_fetch_assoc($questions)); ?>

the form gets the questions from a table in my database (called questions). they are in three categories and only one categories set of questions is pulled at any one time.

I need to insert each answer individually into another table (called user_answers).

I'm having trouble working out quite how to go about this and it's beginning to do my head in!

the form manages to pass through $_POST a value that when I print_r($_POST); I get

Array ( [answers_90_5] => 3 [answers_91_5] => 3 )

With 90 & 91 being the question ids and 5 being the subcategory in both instances.

From here I am stumped.

How do I identify these parts of each individual post along with the answer and insert them as a row each in the table?

I'm fine with doing the MySQL insert normally and a standard form passing values etc. but I'm completely stumped on how to do this!

Many thanks in advance!

3
  • consider that your loop is backwards. You don't fetch a row of data until AFTER you've output a chunk of html, so you'll always be outputting one empty block. this should be a while($row = ...) { ... } loop instead. Commented Nov 7, 2012 at 20:59
  • Not sure I understand where you are stuck. Can't you simply loop over the $_POST data and craft the inserts? All the needed data is there. Am I missing something? Commented Nov 7, 2012 at 21:02
  • crafting the inserts is where my brain is failing me. I think the answer is right in front of my I just can't think how to construct it. Commented Nov 7, 2012 at 21:06

3 Answers 3

3

Using a multi-dimensional array should ease your job of iteration:

<input 
    type="radio" 
    name="answers[<?php echo $row_questions['id']; ?>][<?php echo $row_questions['sub_category']; ?>]" 
    value="1" 
    id="answers_<?php echo $row_questions['id'] . '_' . $row_questions['sub_category']; ?>" />

Debugging this should give you $_POST filled with a array of the likes:

Array ( [answers] => Array(
     90 => Array ( 5 => 3 ),
     91 => Array ( 5 => 3 ),
))
Sign up to request clarification or add additional context in comments.

3 Comments

This will help you immensely, rather than having to parse out these arrays from you input name they way you would need to in your original form code.
Edited answer to also give your input a dynamic id property, as these should be unique for the entire document.
Correct. To explain the general idea: <input name="foo[bar]"> results in $_POST being a multi-dimensional array.
0

sure, separate your queries, post val one and sub cat one for query 1, and query 2 do the same for cat 2 and sub cat two.

very simple.

Comments

0

I think the other answer suggesting multi-dim arrays makes a lot of sense. Thought I would still provide an example on how to parse the string though. It could be something as simple as:

foreach ($post as $k => $v) {
    $parts = explode('_', $k);
    $questionId = $parts[1];            
    $questionCategoryId = $parts[2];    
    $sql = "INSERT INTO answers (questionId, subCategoryId, answerValue ) VALUES ($questionId, $questionCategoryId, $v)";

    ....
}

... only hopefully with better escaping on the POST data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.