It is inserting wrong numbers while using INSERT VALUES

Go To StackoverFlow.com

0

I have a problem with my INSERT VALUES. The problem is when I try and insert question numbers. Lets say I try to insert 2 questions. Well for first question the question number (QuestionId) should be 1 and for second question it should be 2.

But the problem is that both questions display question number 3 when I INSERT VALUES. So what it is doing for every question is inserting what would be the next question number which in this example is '3' for all questions.

Below is the example of what it is displaying at the moment when I echo $questionsql for 2 questions:

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

The above is incorrect. Below is what it should of echoed:

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

So what I want to know is how can I display the correct question numbers for each question, it should add the correct question number in the correct order from question 1, 2 ,3 etc.

Below is the javascript code and form code where it appends a question into a table row. The user appends a question into a table row. When user adds first question it appends question number 1 and the question, when they append their second question it appends question number 2 and the question and etc.

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

Below is the php code where it INSERT VALUES.

$i = 0;

$insertquestion = array();

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

   $insertquestion[] = "'".  mysql_real_escape_string( $_POST['numQuestion'] ) ."','".  
                    mysql_real_escape_string( $_POST['questionText'][$i] ) ."'";

}

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

    echo($questionsql);
2012-04-05 02:32
by user1309180


0

I believe you need to set the value of the form ie. value="1"

<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="1" 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>
2012-04-05 02:38
by RyanS


0

The code in your for() loop has a syntax error; this code doesn't parse. Assuming you fix that and are looping correctly, your problem might be that you are inserting $_POST['questionNum']. From what I can see, the value of this variable would be the total number of questions, not the number of the individual question.

Instead you might use just the value of $i, e.g.

for($i = 0; $i++; $i < count($_POST['questionText']) ){

   $insertquestion[] = "'". $i ."','".  
                mysql_real_escape_string( $_POST['questionText'][$i] ) ."'";

}
2012-04-05 02:43
by Seldo
I just want to know that because I can create multiple exams, in first exam if I create 5 questions then it will display question numbers 1 to 5, but if I then create second exam which is also 5 questions, with your example will it state those question numbers also 1-5 or 6 -10. That's why I think its better just to be able to display the numQuestion from the application so that it displays correct question numbers - user1309180 2012-04-05 03:00


0

You can access a POST variable as array ($_POST['questionText'][$i]) because you used a little trick here:

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

(you missed an ; at the end of line btw) With this line you are generating a HTML like:

<textarea name="dunnowhat[]"></textarea>
<textarea name="dunnowhat[]"></textarea>
<textarea name="dunnowhat[]"></textarea>
<textarea name="dunnowhat[]"></textarea>

See those []'s? That what makes this work. But with the question number you are not using this same trick. Now the bigger problem, even if you used, it might not be the same. Imagine what would happen, if you skipped a value. It might not show up in the POST variable array as empty, so it might mess up the pairing of questions and question numbers by index (the array items would be shifted).

So instead of using this trick I recommend using name spacing. Have an index count, and append that instead of the []. So it would become something like this:

<input type="text" name="question-index.1">3</input>
<textarea name="dunnowhat.1"></textarea>
<input type="text" name="question-index.2">5</input>
<textarea name="dunnowhat.2"></textarea>
<input type="text" name="question-index.3">4</input>
<textarea name="dunnowhat.3"></textarea>
<input type="text" name="question-index.4">1</input>
<textarea name="dunnowhat.4"></textarea>

Or even simpler, to append those id's directly to the name (with jquery)

<textarea name="dunnowhat.3"></textarea>
<textarea name="dunnowhat.5"></textarea>
<textarea name="dunnowhat.4"></textarea>
<textarea name="dunnowhat.1"></textarea>

Doing a foreach on $_POST array checking if the key starts with dunnowhat, you'll get to all the questions.

But even with all these. Why not just use MySQL's AUTO_INCREMENT? If you don't need the user to specify the question number that would solve the whole problem. As for a complete code... I won't write one, there's too much to be changed here.

2012-04-05 02:48
by Máthé Endre-Botond
This looks complicated for what it should be. I just want to know how I can display numQuestion in the INSERT VALUES correctly, that is all I am asking. P.S I can't use auto increment because I might create 2 sessions and if I create 2nd session, I want question number to start back at 1 - user1309180 2012-04-05 03:03
Ads