How to display the correct question number in the database

I have a problm on knowing how to display the correct question number. Below I have a piece of code (not whole code) where when a user clicks on the "Add" button, it appends rows into a table. Each row that is appended contains 2 columns, One is question number and the other column is the file input:

   
 
    var qnum = 1;
    var $tbody = $('#qandatbl > tbody'); 
    var $tr = $("<tr class='optionAndAnswer' align='center'>");
    var $qid = $("<td class='qid'></td>" ).text(qnum);
    var $image = $("<td class='image'></td>"); 
    
        var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return imageClickHandler(this);' class='imageuploadform' >" + 
            "Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><br/><label class='imagelbl'>" + 
        "<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>" + 
        "<label><input type='button' name='imageClear' class='imageClear' value='Clear File'/></label>" +
        "</p></form>");	
    
    $tr.append($qid);
    $tr.append($image);
    $tbody.append($tr);	
    
    
        ++qnum;
        $(".questionNum").text(qnum);
        $(".num_questions").val(qnum);

Open in new window


The table the above code appends to are below:

<table id="qandatbl" align="center">
<thead>
<tr>
    <th></th>
    <th class="qid">Question No</th>
    <th class="image">Image</th>
</tr>
</thead>
<tbody>
</tbody>
</table>

Open in new window


So lets say I click on the "Add" button 3 times, then under the Question number column it will display 3 question numbers, '1' in the first row, '2' in the second row and '3' in the third row.

Now if I use the file input in the same row as question number 2 or lets say I use the file input in the same row as question number 3, then in the database how can I store the correct question number in the database which corresponds with the same row as the file input used?

Below is the code I currecntly have on inserting the data into the database (not whole code, just main code):

    $lastID = $mysqli->insert_id;         
     
     $imagequestionsql = "INSERT INTO Image_Question (ImageId, SessionId, QuestionId)  
        VALUES (?, ?, ?)"; 
        
         if (!$insertimagequestion = $mysqli->prepare($imagequestionsql)) { 
          // Handle errors with prepare operation here 
           echo "Prepare statement err imagequestion"; 
        } 
        
    $qnum = 1;
    
    $insertimagequestion->bind_param("isi",$lastID, $sessid, $qnum); 
    
    $sessid =  $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : ''); 
        
        $insertimagequestion->execute(); 
        
                    if ($insertimagequestion->errno) { 
              // Handle query error here 
            } 
     
            $insertimagequestion->close(); 

Open in new window

carlbrooks1995Asked:
Who is Participating?
 
Derek JensenConnect With a Mentor Commented:
Let me take a stab at this:

You're wanting to upload a file associated with each and every question added to your form, correct? And you're wanting to know how to associate filename & DB record, right?

If so, it's very simple: All you have to do is give each filename submitted on the form a unique name(you can even make a new table in your DB that associates old & new filenames, for the user's benefit, if you want) and then *save that file using that new, unique filename*, and then store the *new* filename in your DB, along w/associated question number. Thus, your table would only need 2 columns: Question number, and file name.

Then, for retrieval, all you'd have to do is
SELECT * FROM Image_Question WHERE QuestionID = {$somevar}

Open in new window

or whatever, and then...do whatever you wanted from there. :)

If you did have a filename lookup table, you could do it in 2 queries or 1, depending on your mood that day, but you could do something like
SELECT * FROM Image_Question iq, File_Lookup fl WHERE iq.FileName = fl.NewFileName

Open in new window

to do it in one. :-)
0
 
Mark BradyPrincipal Data EngineerCommented:
Can you provide more information? You give us a small piece of code that is part of a class object. We can't see what any of the class methods do. In saying that, why don't you simply add another column to the table called 'q_num' or something so the question number is part of your insert statement? Then you can recall it when you recall the question or whatever you are trying to retrieve.
0
 
carlbrooks1995Author Commented:
I've requested that this question be deleted for the following reason:

I want to delete this question because I ant to rewrite the question so that it is easier to understand and that I have more chances of recieving better help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.