【发布时间】:2018-12-29 05:44:39
【问题描述】:
我正在尝试制作一个小型图像/测验应用程序,允许用户上传问题,而其他用户回答这些问题。其他用户最多可以为他们的答案选择三个复选框。一旦被选中,该数据将被发布到一个脚本,该脚本将根据具有正确答案的数据库检查猜测。许多问题都在同一个页面上,当用户上传数据时,更多问题会通过 php 脚本回显到页面上。为了确保每个问题都有一个唯一的标识符,我为测验表单设置了一个隐藏输入,该输入采用循环给出的值。第一个问题很好用!但是,其他问题无法回答,因为隐藏输入似乎只取循环中最后一个元素的值(降序)。此外,POST 机制似乎也只关心最近的项目。
以下是将图像和问题循环到页面的代码:
$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>';
}
}
?>
请注意,'.$row["imageFullNameGallery"].' 位于上述代码中的<p class="desc"> 标记下。这正确地给了我每个图像的全名。我这样做只是为了测试这里是否有错误。不过没有。
这是处理隐藏输入后的代码(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!";
}
?>
我做了一些研究,有人认为这可能是因为隐藏输入的所有名称都是相同的,因此它只与从循环传递的最终值一起使用。如果是这种情况,如何解决这个问题,以便每个项目都有其隐藏输入的正确值?另外,关于为什么只发布第一个表单的输入有什么想法吗? (记住顺序是降序的,所以它是从隐藏输入中获得正确值的表单;从循环中给出的最后一个值)。感谢您花时间阅读(希望能帮助我寻找真相)我冗长的问题。谢谢!
【问题讨论】: