在图像上载过程中未正确传递变量


Variable not being passed properly during image upload

我试图在页面上的每个食谱旁边都有一个上传按钮,所以我需要在图像上传过程中传递食谱ID值以便将图像路径保存在数据库中的每个食谱旁。但由于某些原因,我不断收到同样的错误:

 Notice: Undefined index: recipe_id in C:'xampp'htdocs'upload.php on line 4  

第4行为:$recipe_id = $_POST['recipe_id'];

这是HTML表单:

<div class="upload_icon">
  <form id="<?php echo $recipe_id ?>" action="upload.php" method="POST"   
  enctype="multipart/form-data">
    <input type="file" name="image"  value="<?php echo $recipe_id ?>"/></input>
    <button type="submit" name="submit" value="add">Add image!</button>
  </form>
</div>

PHP部分(upload.PHP):

if ($_SERVER["REQUEST_METHOD"] == "POST");
  {
   $recipe_id = $_POST['recipe_id'];
   $name = $_FILES ['image'] ['name'];
   $tmp_name = $_FILES ['image'] ['tmp_name'];   
   $location = "uploads/$name";
   move_uploaded_file($tmp_name, $location);
   $update = query("UPDATE menu SET recipe_pic = '".$location."' WHERE recipe_id =   
  '$recipe_id' " );
  }   
?>

我做错了什么?

在渲染的HTML中没有提到recipe_id。试试这个:

<input type="hidden" name="recipe_id" value="<?php echo $recipe_id ?>">
<input type="hidden" name="recipe_id" value="<?php echo $recipe_id; ?>">

您没有向PHP脚本传递名称为recipe_id的变量-表单中没有该名称的输入

<form id="<?php echo $recipe_id ?>" action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input name="recipe_id" type="hidden" value="<?php echo $recipe_id ?>">
<button type="submit" name="submit" value="add">Add image!</button>
</form>