I am trying to pick out the correct question number from a table and inserting it in the database. Lets say I have a table where it appends rows and that I have 3 rows, the table below will look something like this:
Question No Image
1 (file input)
2 (file input)
3 (file input)
Below is the code that creates the table above:
var qnum = 1;
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);
++qnum;
$(".questionNum").text(qnum);
$(".num_questions").val(qnum);
What my question is that lets say I use the file input in row 2 for example, how can I insert the question number within the same row as that file input to be inserted into the database (the question number inserted would obviously be 2)?
Another example is that if I use the file input in row 3 for example, how can I insert the question number within the same row as that file input to be inserted into the database (the question number inserted would obviously be 3)?
Below is the code I currecntly have on inserting the data into the database (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();
At the moment the code above is just inserting number 1 each time for question number in the database
If I understood your question correctly, here’s the nice way to do it: PHP supports “subarrays” in $_POST, $_GET and $_FILES. I.E. if you have
You could easiy check which file upload was triggered with $_FILES[“upload”][X], where X is between 1 and 3. Same goes for $_GET and $_POST. Remember this trick because it’s very useful, especially if appending elements on the fly with JavaScript. E.g. you could have a code like:
Then, in the PHP code you could check the length of the $_FILES[“upload”] array, and even get its keys with array_keys and check which file input has been “uploaded”. Hope this helps.