0

我在这里有一个 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);
4

1 回答 1

2

我在您的 jsfiddle 中注意到,每个新创建的单选框都会像回复 1 和回复 2 一样创建。

但是在您的 php 代码中,看起来您正在循环,就好像它是一个数组一样。

如果你对你的帖子值进行 print_r ,你会得到类似的东西

Array ( [reply1] => Single [reply2] => Multiple )     

所以它们不是数组格式。如果您在 POST 请求中拥有的唯一值是单选按钮,那么您可以循环遍历 POST 数组值。

无论如何,这是一个可能的解决方案,供您遍历您的帖子请求。我只是基于您在 jsfiddle 中的示例。您可能必须适应您的实际代码。但他们的关键是要注意单选按钮的名称命名为reply[0]reply[1]。PHP 知道将名为 like 的值放入数组中。

<?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);
于 2012-04-09T01:33:27.600 回答