如何在下面的示例中发布隐藏的输入

时间:2012-10-05 16:48:18

标签: php mysqli

我下面有一张表,其中包含问题编号,回复和答案。让我们说下表如下:

QuestionNo   Reply         Answer

1            Single        A D
2            Multiple      B E F
3            Multiple      C
4            Single        D H

我想要做的是$ _POST这些值,然后我可以将它们插入数据库。

我想在相关问题中插入每个答案,因此我创建了一个for循环。但我的问题是如何编写switch语句以便为每个问题发布正确的答案?

下面是for循环和将插入答案值的mysqli代码,我只需要帮助编写switch语句:

    $i = 0;
    $c = count($_POST['gridValues']);  //count each table row

    for($i = 0;  $i < $c; $i++ ){

   //switch statement for each answer post goes here

    }

   $answersql = "INSERT INTO Answer (Answer) 
    VALUES (?)";

      if (!$insertanswer = $mysqli->prepare($answersql)) {
      // Handle errors with prepare operation here
    }  

    $insertanswer->bind_param("s", $_POST[''][$i]);

        $insertanswer->execute();

        if ($insertanswer->errno) {
          // Handle query error here
        }

        $insertanswer->close();

    }

每个答案都有自己的名称属性,有30个不同的答案,以下是如何发布的示例:

$_POST['answerAvalue']  //Answer A
$_POST['answerBvalue']  //Answer B
$_POST['answerCvalue']  //Answer C
$_POST['answerDvalue']  //Answer D

....

$_POST['answerTruevalue']  //Answer True
$_POST['answerFalsevalue'] //Answer False
$_POST['answerYesvalue']   //Answer Yes
$_POST['answerNovalue']    //Answer No

1 个答案:

答案 0 :(得分:1)

疯狂的问题,但我有一个建议,改变这个:

switch ($_POST['reply'][$i]){ 

case "single": 
$selected_reply = "Single"; 
break; 

case "multiple": 
$selected_reply = "Multiple"; 
break; 

default: 
$selected_reply = ""; 
break; 

}

关于这个:

$selected_reply = "";
if (isset($_POST['reply'][$i])) {
    $selected_reply = ucfirst($_POST['reply'][$i]);
}