0

我的代码有两个问题:

问题1:

对于 questionId,它没有显示正确的问题编号。假设我有 2 个问题。那么对于问题 1,questionId 应该是 '1' 但它会显示 '3' 另一个例子是,如果我有 7 个问题,那么对于问题 1,questionId 应该是 '1' 但它会显示 '8'。

如何解决这个问题?

问题2:

我的 $questionsql 只回显 1 个问题,即使我有多个问题要回显。我做错了什么,我可以显示所有问题,而不仅仅是 1 个问题。

例子:

如果我有两个问题是:

Question 1: 

SessionId   QuestionId   QuestionContent    OptionId

ABV         1            What is my name?   O6   

Question 2: 

SessionId   QuestionId   QuestionContent    OptionId

ABV         2            What is my age?   O9

然后在 echo $questionsql 它应该显示:

INSERT INTO Question (SessionId, QuestionId, QuestionContent, OptionId) VALUES ('ABV' ,'1','What is my name?','O6'), ('ABV' ,'2','我的年龄? ','O9')

但它没有显示上面的内容,而是显示下面的内容,这是不正确的,因为它显示了错误的问题编号,并且只显示了 1 个问题,这是最新添加的问题。

插入问题(SessionId,QuestionId,QuestionContent,OptionId)值('ABV','3','我的年龄是多少?','O9')

更新

下面是代码,如何解决这两个问题:

  foreach($_POST['questionText'] as $i => $question)
{

      $insertquestion = array();


$options[] = $_POST['gridValues'];

switch ($options[$i]){

    case "3": 
    $selected_option = "A-C";
    break;

    case "4": 
    $selected_option = "A-D";
    break;

    case "5": 
    $selected_option = "A-E";
    break;

    default:
    $selected_option = "";
    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']; 


    $insertquestion[] = "'". mysql_real_escape_string( $_SESSION['id'] ) . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '') ."' ,'". mysql_real_escape_string( $_POST['num_questions'] ) ."','".  mysql_real_escape_string( $question ) ."','".  mysql_real_escape_string( $optionid ) ."'";

    $questionsql = "INSERT INTO Question (SessionId, QuestionId, QuestionContent, OptionId) 
    VALUES (" . implode('), (', $insertquestion) . ")";

    $i++;

    }

echo($questionsql);

下面是表格代码。它的工作原理是用户在文本区域('name='questionText')中输入一个问题并输入一个选项(name='gridValues'),然后他们将它们两个附加到一个表格行中(表格形式为 which id ='qandatbl')。这是问题1。然后他们对第二个问题再次做同样的事情,然后是第三个等等。请仔细看这个,很容易理解:)

4

2 回答 2

0

Okay, basically there are two problems here. The first is that on every cycle of your primary loop, you're resetting $i. You need to make it the key way you find the value you want:

foreach($_POST['questionText'] as $i => $question)
{

   $insertquestion = array();

   $options[] = $_POST['gridValues'];

   switch ($options[$i]){

   ...

I'm assuming here that your gridValues input and your questionText input correspond to one another, i.e. for the first gridValue you also want the first questionText.

The second thing which seems wrong is that you're assembling your SQL query as an array. What you need to do is make a separate query for each iteration of your loop:

   $insertquestion = "'". mysql_real_escape_string( $_SESSION['id'] ) . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '') ."' ,'". mysql_real_escape_string( $_POST['num_questions'] ) ."','".  mysql_real_escape_string( $question ) ."','".  mysql_real_escape_string( $optionid ) ."'";

   $questionsql = "INSERT INTO Question (SessionId, QuestionId, QuestionContent, OptionId) 
  VALUES (" . implode('), (', $insertquestion) . ")";

   $i++;

}
于 2012-04-04T00:57:04.987 回答
0

您在语句$i=0;之前和循环内部进行设置,因此您最终只创建了数组中的第一个元素。switchforeach

不用担心!我一直这样做!

于 2012-04-03T23:45:10.373 回答