I am trying to make a small image/quiz application that allows users to upload a question, while other users answer those questions. The other users are allowed to select up to three checkboxes for their answers. Once selected, this data is to be posted to a script that checks the guesses against a database that has the correct answers. Many questions are on the same page, and more are echoed onto the page via a php script as users upload data. To ensure that each question has a unique identifier, I have a hidden input for the quiz form that takes on the value given from a loop. The first question works great! However, the other questions are unanswerable, because the hidden input seems to only take on the value of the last element in the loop(DESCENDING order). Also, the POST mechanism only seems to care about the most recent item as well.
Here is the code which loops out images and questions to the page:
$sql = "SELECT * FROM gallery1 ORDER BY idGallery DESC";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL Statment Failed";
}else {
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_assoc($result)){
echo '<div class="maincontainer">
<div class="thecard">
<div class="thefront" style="background-image:url(gallery/'.$row["imageFullNameGallery"].'); background-size:100% 100%;">
</div>
<div class="theback">
<p class="heading"> The Story</p>
<p class="desc">
'.$row["descriptionGallery"].'
'.$row["imageFullNameGallery"].'
</p>
<form action="includes/survey.php" id="form1" method="POST">
<label> <img src="img/IMAGE1.png" class="radio">
<input type="checkbox" name="food" value="1">
</label>
<label> <img src="img/IMAGE2.png" class="radio">
<input type="checkbox" name="water" value="1">
</label>
<label> <img src="img/IMAGE3.png" class="radio">
<input type="checkbox" name="shelter" value="1">
</label>
<input type="hidden" name="image" value='.$row["imageFullNameGallery"].'>
</form>
<button type="submit" form="form1" name="submit">Submit </button>
</div>
</div>
</div>';
}
}
?>
Notice that the '.$row["imageFullNameGallery"].'
is under the <p class="desc">
tag in the above code. This properly gives me the full name of each image. I did this just to test if there were errors here. There are none though.
This is the code that handles the hidden input once it has been received(survey.php):
<?php
if (isset ($_POST['submit'])) {
include_once "dbh.php";
$food=$_POST['food'];
$water=$_POST['water'];
$shelter=$_POST['shelter'];
$image=$_POST['image'];
$survey = array("foodGallery"=>$food, "waterGallery"=>$water, "shelterGallery"=>$shelter);
$sql= "SELECT foodGallery, waterGallery, shelterGallery FROM gallery1 WHERE imageFullNameGallery = '$image';";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result);
print_r($row);
if ($survey !== $row){
echo "These are not the same";
} else{
echo "These have the same values!";
}
?>
I have done some research, and some suggest that it could be because all of the names for the hidden inputs would be the same, therefore it only goes with the final value passed from the loop. If this is the case, how could this be solved, so that each item has the proper value for its hidden input? Also, any thoughts as to why only the first form's input is being posted? (Remember the order is descending, so it is the form with the true value as being correct from the hidden input; the last value given from the loop). I appreciate you for taking your time to read (and hopefully aid me in my search for truth) my wordy question. THANK YOU!