尽管我定义了一个变量并使用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
并没有得到什么问题。请帮忙。
答案 0 :(得分:0)
发送文件时,您需要指定multipart / form-data。
<form action="" method="post" enctype="multipart/form-data">
有关编码类型编码的更多信息,请参见此helpful post。
您应始终检查isset
并相应地处理结果。