[英]inserting a dynamically generated $_POST values from a form into mysql from php
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). 我需要将每个答案分别插入另一个表(称为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);
表单设法通过$ _POST传递一个值,当我print_r($_POST);
I get 我明白了
Array ( [answers_90_5] => 3 [answers_91_5] => 3 ) 数组([answers_90_5] => 3 [answers_91_5] => 3)
With 90 & 91 being the question ids and 5 being the subcategory in both instances. 90和91是问题ID,5是两个实例中的子类别。
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! 我很好地正常进行MySQL插入和标准表单传递值等但我完全不知道如何做到这一点!
Many thanks in advance! 提前谢谢了!
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: 调试这个应该给你$_POST
填充一个喜欢的数组:
Array ( [answers] => Array(
90 => Array ( 5 => 3 ),
91 => Array ( 5 => 3 ),
))
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. 确定,将您的查询分开,将post one和sub cat one分别用于查询1,查询2对cat 2和sub cat 2执行相同的操作。
very simple. 很简单。
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. ...希望能够更好地逃避POST数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.