I believe jquery increment is causing the wrong number to be inserted into the database
<p>I want the correct question number to be displayed in the database table but problem is that it is displaying incorrect question numbers by +1.</p>

<p>Lets say I have 4 rows and I used file input in row 2 and row 4 to upload the files, then it means the question numbers sohuld be 2 and 4 meaning the table should look like this below:</p>

<pre><code>ImageId    SessionId  QuestionId
01               AAA        2
02               AAA        4
</code></pre>

<p>But instead it has got the question number wrong by only plus 1 meaning it is displaying it like this:</p>

<pre><code>ImageId    SessionId  QuestionId
01               AAA        3
02               AAA        5 
</code></pre>

<p>I believe the problem is that I am not inncrementing the question numbers correctly. Can anybody see what I am doing incorrectly in my code below that it is inserting the wrong question numbers?</p>

<p>Below is the code:</p>

<pre><code>var qnum = 1; //question number starts at 1
var qremain = &lt;?php echo (int)$_SESSION['textQuestion']; ?&gt;; //the full number of questions

var $qid = $("&lt;td class='qid'&gt;&lt;/td&gt;" ).text(qnum);
var $image = $("&lt;td class='image'&gt;&lt;/td&gt;");    

var $fileImage = $("&lt;form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return imageClickHandler(this);' class='imageuploadform' &gt;" + 
        "Image File: &lt;input name='fileImage' type='file' class='fileImage' /&gt;&lt;/label&gt;&lt;br/&gt;&lt;br/&gt;&lt;label class='imagelbl'&gt;" + 
    "&lt;input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /&gt;&lt;/label&gt;" + 
    "&lt;label&gt;&lt;input type='button' name='imageClear' class='imageClear' value='Clear File'/&gt;&lt;/label&gt;" +
    "&lt;/p&gt;&lt;/form&gt;"); 


$tr.append($qid);
$tr.append($image);

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

    var $this = $(this);

    var $questionNumber = $("&lt;input type='hidden' class='num_questionsRow'&gt;").attr('name',$this.attr('name')+"[]")
                   .attr('value',$this.val());

    $qid.append($questionNumber);                             

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

    --qremain;
    $(".questionRemain").text(qremain);
});
</code></pre>

<p>How the <code>$('.num_questions').each( function() {</code> work is like this:</p>

<ul>
<li>When user opens page, qnum (question number) starts with '1' at the top</li>
<li>When user appends a table row, then it will take question number '1' from the top and display it within the added row and the qnum (question number) at the top increments so that it now states 2</li>
<li>When user appends another row, it will take question number '2' from the top and display it within the added row and the qnum (question number) at the top increments so it now displays 3.</li>
<li>And so and so on...</li>
</ul>
