I am getting an undefined index
notice in my php code though I defined a variable and submitting it with POST method. Here's the code:
<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 code:
<?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>";
}
}
}
?>
I have already defined a variable and also submitting it via POST methd, but still I am getting error Notice: Undefined index: image_src in C:\wamp64\www\arsod_dynamic\admin\home.php on line 368
not getting what is problem. Please help.
You need to specify multipart/form-data when sending files.
<form action="" method="post" enctype="multipart/form-data">
See this helpful post for more information on enctype encoding.
You should always check for isset
and handle the result accordingly.