我在这里有一个 jsfiddle 。发生的情况是,如果您选择一个单选按钮并单击“添加问题”,它将添加一个表格行,显示您选择的单选按钮。您可以更改行内的选择。
现在我想要做的是我想通过使用 INSERT VALUES 方法在数据库中的每个单选按钮中插入选定的单选按钮。
所以我想知道的是我怎样才能正确地做到这一点,以便 $_POST 为每一行选择的单选按钮,然后能够使用 INSERT VALUES 插入它们?
下面是我目前拥有的php代码:('questionText'是'Question'列,即使我没有在jsfiddle中包含'questionText'并且'gridValues'不在jsfiddle中但是它实际上选择了每一行这是针对“选项”列中每一行中的每个文本框值,所以想象一下表格中还有另外两列,即“问题”和“选项”列)
$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);