?
Solved

How to display the correct question number in the database

Posted on 2012-09-19
3
Medium Priority
?
246 Views
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>" +
        "</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

0
Comment
Question by:carlbrooks1995
3 Comments
 
LVL 20

Expert Comment

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

Author Comment

by:carlbrooks1995
ID: 38419185
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
 
LVL 9

Accepted Solution

by:
Derek Jensen earned 2000 total points
ID: 38419186
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

839 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