How to display the correct question number in the database

Posted on 2012-09-19
Last Modified: 2012-09-20
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>" +

Open in new window

The table the above code appends to are below:

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

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'] : ''); 
                    if ($insertimagequestion->errno) { 
              // Handle query error here 

Open in new window

Question by:carlbrooks1995
    LVL 20

    Expert Comment

    by:Mark Brady
    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.

    Author Comment

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

    Accepted Solution

    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 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. :-)

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now