0

我的插入值有问题。问题是当我尝试插入问题编号时。假设我尝试插入 2 个问题。那么对于第一个问题,问题编号(QuestionId)应该是 1,对于第二个问题应该是 2。

但问题是当我插入值时,这两个问题都显示问题编号 3。所以它对每个问题所做的就是插入下一个问题编号,在这个例子中,所有问题都是“3”。

下面是当我为 2 个问题回显 $questionsql 时它显示的示例:

INSERT INTO Question (QuestionId, QuestionContent) VALUES ('3','what is my name'), ('3','what is my age') 

以上是不正确的。以下是它应该回应的内容:

INSERT INTO Question (QuestionId, QuestionContent) VALUES ('1','what is my name'), ('2','what is my age') 

所以我想知道的是如何为每个问题显示正确的问题编号,它应该按照问题 1、2、3 等的正确顺序添加正确的问题编号。

下面是将问题附加到表格行中的 javascript 代码和表单代码。用户将问题附加到表格行中。当用户添加第一个问题时,它会附加第 1 个问题和问题,当他们附加第二个问题时,它会附加第 2 个问题和问题等。

<script>

    function insertQuestion(form) {   

    var $tbody = $('#qandatbl > tbody'); 
    var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
    var $qid = $("<td class='qid'>" + qnum + "</td>");
    var $question = $("<td class='question'></td>");


    $('.questionTextArea').each( function() {

    var $this = $(this);
    var $questionText = $("<textarea class='textAreaQuestion'></textarea>").attr('name',$this.attr('name')+"[]")
                   .attr('value',$this.val())

    $question.append($questionText);

    });

    $tr.append($qid); 
    $tr.append($question);   
    $tbody.append($tr); 

    }

    ++qnum;
    $(".questionNum").text(qnum);
    $(".num_questions").val(qnum);

</script>


<form id="QandA" action="insertQuestion.php" method="post" >

<table id="question">
<tr>
    <th colspan="2">
        Question Number <span class="questionNum">1</span>
        <input type="hidden" class="num_questions" value="" name="numQuestion">
    </th>
</tr>
<tr>
    <td rowspan="3">Question:</td> 
    <td rowspan="3">
        <textarea class="questionTextArea" rows="5" cols="40" name="questionText"></textarea>
    </td>
</tr>
</table>

</form>

下面是插入值的 php 代码。如何让它正确显示 numQuestion。我不能做一个简单的计数循环,因为我可能有 2 次考试,所以在为第一次考试创建问题后,在第二次考试中我需要问题编号再次从“1”开始,所以我认为最好正确显示“numQuestion”。

    $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;

    }      

    $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( $_POST['numQuestion'] ) ."','".  
                    mysql_real_escape_string( $_POST['questionText'][$i] ) ."','".  
                    mysql_real_escape_string( $optionid ) ."'";

}

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

echo($questionsql);


mysql_close();

4

2 回答 2

0

you dont have to generate these question numbers. try using an auto increment sequence in the database. you are confusing some things here. You need a unique identifier in your database, which is a different thing as the numbers of the table in your frontend. also, if you have different exams with different questions, you should have 2 tables, one holding you exams, and one holding your questions with an auto increment id and a foreign key that references the exams it belongs to. In this scenario you dont have to commit the question ids but only query the question name (and the exam refernce), the database will generate your Id. the question count value in your table needs to be independent from your database Id.

于 2012-04-05T13:32:22.973 回答
0

如果它是主键,则保留该字段,如下所示

INSERT INTO Question (QuestionContent) VALUES ('what is my name'), ('what is my age')

它会自动将 id 分配给问题。当您获得要显示的数据时,您可以简单地使用变量来生成问题编号。您将不得不避免使用 id 作为问题编号。

于 2012-04-05T13:36:19.573 回答