为什么有未定义的索引通知虽然变量已定义?

why there is undefined index notice though variable is defined?

尽管我定义了一个变量并使用 POST 方法提交,但我在 php 代码中收到 undefined index 通知。这是代码:

<form action="" method="post">
<button class="subbutton button1" type="button" onclick="return 
toggleMe('image')">Update Image</button><br>
<div id="image" style="display:none">
<center>
<table style="width:80%;">
<tr>
<td><input type="text" name="img_rn" placeholder="Enter Roll No." 
class="textstyle" required="required"/></td>
<td><input type="file" name="image_src" id="fileToUpload" 
required="required" style="color:white;"/></td>
<td><button class="button button1" type="submit" name="img_upld" 
class="textstyle" required="required">Upload</button></td>
</tr>
</table>
</center>
</div>
</form>

php代码:

<?php
if(isset($_POST["img_upld"]))
{
$img_rn=$_POST["img_rn"];
$cname = $_FILES['image_src']['name'];
 if(!empty($cname)){
 $tname = $_FILES['image_src']['tmp_name'];
 $size  = $_FILES['image_src']['size'];
 $fext = pathinfo($cname, PATHINFO_EXTENSION);

// Allow certain file formats
if($fext != "jpg" && $fext != "png" && $fext != "jpeg"
&& $fext != "gif" ) {
    echo "<center>Sorry, only JPG, JPEG, PNG & GIF files are allowed. 
</center>";
}

$savename = $img_rn.".".$fext;
}
$finalfile = "../uploads/$savename";
 if(!empty($cname)){
 if($size < 500000){
    $check = move_uploaded_file($tname,$finalfile);
    if($check){
    $upld=$conn->prepare("UPDATE users SET image=:savename WHERE 
 rollno=:img_rn");
    //binding
    $upld->bindparam(":savename",$savename);
    $upld->bindparam(":img_rn",$img_rn);
    $result=$upld->execute();

    echo "<center style='color:white'>File uploaded successfully!</center>";
    } else{
        echo "<center style='color:white'>File upload failed.</center>";
    }
  }
     else{
     echo "<center style='color:white'>File size too large.</center>";
  }
  }
  }
  ?>

我已经定义了一个变量并通过 POST 方法提交了它,但我仍然收到错误 Notice: Undefined index: image_src in C:\wamp64\www\arsod_dynamic\admin\home.php on line 368 没有得到什么问题。请帮忙。

发送文件时需要指定multipart/form-data。

<form action="" method="post" enctype="multipart/form-data">

有关 enctype 编码的更多信息,请参阅此 helpful post

您应该始终检查 isset 并相应地处理结果。