Reputation: 21
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!
Upvotes: 1
Views: 1304
Reputation: 7063
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.
Upvotes: 0
Reputation: 2565
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 ),
))
Upvotes: 3
Reputation: 1365
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.
Upvotes: 0