0

我在下面有一个表格,其中包含问题编号、回复和答案。假设该表如下所示:

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
4

1 回答 1

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]);
}
于 2012-10-05T16:52:45.347 回答