我有一个jsfiddle here。如果您选择一个单选按钮并单击“添加问题”,它将添加一个表格行,显示您选择的单选按钮。您可以更改行中的选择。
现在我想做的是我想使用INSERT VALUES方法在数据库中的每个按钮中插入所选的单选按钮。
所以我想知道的是我如何才能正确地执行此操作,以便$ _POST每行的选定单选按钮,然后能够使用INSERT VALUES插入它们?
下面是我目前拥有的PHP代码:('questionText'是'问题'列,它实际上挑选了每一行,即使我没有在jsfiddle中包含'questionText'并且'gridValues'不在jsfiddle,但是对于'Options'列中每行的每个文本框值,所以想象一下表中还有两列是'Question'和'Options'列。)
$i = 0;
$c = count($_POST['gridValues']);
$insertquestion = array();
for($i = 0; $i < $c; $i++ ){
switch ($_POST['gridValues'][$i]){
case "3":
$selected_option = "A-C";
break;
case "4":
$selected_option = "A-D";
break;
default:
$selected_option = "";
break;
}
foreach($_POST['reply'] as $reply) {
switch ($_POST['reply']){
case "Single":
$selected_reply = "Single";
break;
case "Multiple":
$selected_reply = "Multiple";
break;
default:
$selected_reply = "";
break;
}
}
$optionquery = "SELECT OptionId FROM Option_Table WHERE (OptionType = '". mysql_real_escape_string($selected_option)."')";
$optionrs = mysql_query($optionquery);
$optionrecord = mysql_fetch_array($optionrs);
$optionid = $optionrecord['OptionId'];
$replyquery = "SELECT ReplyId FROM Reply WHERE (ReplyType = '". mysql_real_escape_string($selected_reply)."')";
$replyrs = mysql_query($replyquery);
$replyrecord = mysql_fetch_array($replyrs);
$replyid = $replyrecord['ReplyId'];
$insertquestion[] = "'".
mysql_real_escape_string( $_POST['questionText'][$i] ) ."','".
mysql_real_escape_string( $optionid ) ."','".
mysql_real_escape_string( $replyid ) ."'";
}
$questionsql = "INSERT INTO Question (QuestionContent OptionId, ReplyId)
VALUES (" . implode('), (', $insertquestion) . ")";
echo($questionsql);
答案 0 :(得分:2)
我注意到你的jsfiddle中每个新创建的单选框都被创建为reply1和reply2。
但是在你的php代码中,它看起来像是一个循环,好像它是一个数组。
如果您对帖子值执行print_r,则会得到类似
的内容Array ( [reply1] => Single [reply2] => Multiple )
所以它们不是数组格式。如果POST请求中的唯一值是单选按钮,则可以循环POST数组值。
无论如何,这里有一个可能的解决方案,让你循环你的帖子请求。我只是根据你在jsfiddle的例子。您可能必须适应您的实际代码。但他们的关键是要注意,单选按钮的名称被命名为reply[0]
和reply[1]
。 PHP知道将名称中的值命名为数组。
<?php
//This is what your values will look like. Notice that they are in array format now
print_r($_POST);
foreach($_POST['reply'] as $reply) {
//Now you can loop through your replies correctly.
}
?>
<!-- Quick Example of how to name the radio buttons -->
<html>
<head></head>
<body>
<form method="post">
Row 1<br />
<input type="radio" value="Single" name="reply[0]" /> Single
<input type="radio" value="Multiple" name="reply[0]" /> Multiple
<br />Row 2<br />
<input type="radio" value="Single" name="reply[1]" /> Single
<input type="radio" value="Multiple" name="reply[1]" /> Multiple
<br />
<input type="submit" value="Submit" />
</form>
</body>
以上代码仅针对回复重写。由于我不确定数据是如何格式化或数据实际存在的,因此我将其他内容留下了。我想知道为什么你不只是使用id为收音机盒值而不是文本。这样您就不必继续查询数据库了。您可以只查询所有回复类型,然后匹配id。
<?php
$insertquestion = array();
foreach($_POST['reply'] as $reply) {
switch ($reply){
case "Single":
$selected_reply = "Single";
break;
case "Multiple":
$selected_reply = "Multiple";
break;
default:
$selected_reply = "";
break;
$replyquery = "SELECT ReplyId FROM Reply WHERE (ReplyType = '". mysql_real_escape_string($selected_reply)."')";
$replyrs = mysql_query($replyquery);
$replyrecord = mysql_fetch_array($replyrs);
$insertquestion[] = $replyrecord['ReplyId'];
}
$questionsql = "INSERT INTO Question (ReplyId)
VALUES (" . implode('), (', $insertquestion) . ")";
echo($questionsql);
除了使用for循环之外,这里基本相同。我猜你不熟悉foreach循环。但在这种情况下,for循环也可以为你提供更好的服务。
<?php
$insertquestion = array();
for($i = 0; $i < count($_POST['reply']), $i++) {
switch ($_POST['reply'][$i]){
case "Single":
$selected_reply = "Single";
break;
case "Multiple":
$selected_reply = "Multiple";
break;
default:
$selected_reply = "";
break;
$replyquery = "SELECT ReplyId FROM Reply WHERE (ReplyType = '". mysql_real_escape_string($selected_reply)."')";
$replyrs = mysql_query($replyquery);
$replyrecord = mysql_fetch_array($replyrs);
$insertquestion[] = $replyrecord['ReplyId'];
}
$questionsql = "INSERT INTO Question (ReplyId)
VALUES (" . implode('), (', $insertquestion) . ")";
echo($questionsql);