<form class="formp" action="" method="post" enctype="multipart/form-data" id="frm" name="wjd">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"/>
<p>Gerecht:</p><p>Prijs:</p><p>Plaatje:</p><p>Upload:</p>
<?php
$resultmenus = mysqli_query($dbc,"SELECT * FROM prijslijst WHERE menunaam='menu\'s'");
while($rowmenus = mysqli_fetch_array($resultmenus,MYSQLI_ASSOC)) {
echo "<input type=\"hidden\" name=\"id\" value=\"{$rowmenus['id']}\"/>";
echo "<input name=\"gerecht[]\" type=\"text\" size=\"30\" maxlength=\"30\" value=\"{$rowmenus['gerecht']}\"/>";
echo "<input name=\"prijs[]\" type=\"text\" size=\"5\" maxlength=\"5\" value=\"{$rowmenus['prijs']}\"/>";
echo "<img src=\"../{$rowmenus['plaatje']}\" /><input size=\"1\" type=\"file\" name=\"file[]\" id=\"file\"/>";
}
<input type="submit" name="submit" value="verzend" />
</form>
这将从隐藏输入类型字段中的 db echo'd 输出 id 为 1 到 5 的表单。我的问题是:当我在第 3 行中仅上传 1 个带有 id3 的文件而其余文件为空时,是否可以仅检索该 id?
因为当我尝试使用 $_POST['id']; 检索它时 我得到最后一个值 5。这是浏览器的输出。
<form id="frm" class="formp" name="wjd" enctype="multipart/form-data" method="post" action="">
<input type="hidden" value="2000000" name="MAX_FILE_SIZE">
<p>Gerecht:</p><p>Prijs:</p><p>Plaatje:</p><p>Upload:</p>
<input type="hidden" value="1" name="id">
<input type="text" value="Menu 1" maxlength="30" size="30" name="gerecht[]">
<input type="text" value="3.00" maxlength="5" size="5" name="prijs[]">
<img src="../images/plaatjes/mini_menu3.gif">
<input id="file" type="file" name="file[]" size="1">
<input type="hidden" value="2" name="id">
<input type="text" value="Menu 2" maxlength="30" size="30" name="gerecht[]">
<input type="text" value="6.00" maxlength="5" size="5" name="prijs[]">
<img src="../images/plaatjes/mini_menu2.gif">
<input id="file" type="file" name="file[]" size="1">
<input type="hidden" value="3" name="id">
<input type="text" value="Menu 3" maxlength="30" size="30" name="gerecht[]">
<input type="text" value="6.00" maxlength="5" size="5" name="prijs[]">
<img src="../images/plaatjes/mini_menu1.gif">
<input id="file" type="file" name="file[]" size="1">
<input type="hidden" value="4" name="id">
<input type="text" value="Menu 4" maxlength="30" size="30" name="gerecht[]">
<input type="text" value="7.00" maxlength="5" size="5" name="prijs[]">
<img src="../images/plaatjes/mini_menu3.gif">
<input id="file" type="file" name="file[]" size="1">
<input type="hidden" value="5" name="id">
<input type="text" value="Menu 5" maxlength="30" size="30" name="gerecht[]">
<input type="text" value="4.00" maxlength="5" size="5" name="prijs[]">
<img src="../images/plaatjes/mini_menu2.gif">
<input id="file" type="file" name="file[]" size="1">
<input type="submit" value="verzend" name="submit">
</form>
这是处理脚本。
if(isset($_POST['submit'])) {
if((array_search('', $_POST["gerecht"])!==false) or (array_search('', $_POST["prijs"])!==false)) { //1 or more field empty
echo "<span class=\"error\">Please fill in all fields</span>";
exit();
}else{//field filled in
foreach ($_FILES['file']['name'] as $i => $name) {
if ($_FILES['file']['error'][$i] == 4) { // no upload continue
continue;
}
if ($_FILES['file']['error'][$i] == 0) {
echo $_POST['id'];
}//end else error 0
}//end foreach
}//end filled in forms
}else{ //submit button clicked
}