Sabyasachi Gupta Sabyasachi Gupta - 1 year ago 133
HTML Question

Undefined index: image for ($_FILES['image']['name'])

I have a simple registration form where I accept inputs from the user which includes an image, and insert the values in a table : temporary_employees table . In my code I check whether the email id and the user id entered by the user already exists and if they dont , i go ahead and perform the insert after moving the image to a folder named 'images' . While running the code , I am getting an error Undefined index: image on the line where I have the following piece of code :

$target_file = $target_path . basename ($_FILES['image']['name']);

The most interesting thing is the same line of code has worked perfectly well in another php file . I had given the same name for the input in the html form . . How is it possible ? Any help will be appreciated .

Here is my code :


//start the session before anything is echoed to the browser
if (session_status()===PHP_SESSION_NONE) {

<!DOCTYPE html>
Login form
<h3>Registration Form</h3>
<form action ="" method="POST">
<table align="center" cellpadding="10">
<td><input type="text" maxlength='100' name="empname" id="empname" required></td>
<td>Email Id</td>
<td><input type="text" maxlength='100' name="emailid" id="emailid" required>
<td>User Id</td>
<td><input type="text" maxlength='100' name="userid" id="userid" required ></td>
<td><input type="password" maxlength='100' name="pwd" id="pwd" required ></td>
<td>Date of Birth</td>
<select name='year'>
<option value='1996'>1996</option>
<option value='1997'>1997</option>
<option value='1998'>1998</option>
<option value='1999'>1999</option>
<option value='2000'>2000</option>
<option value='2001'>2001</option>
<option value='2002'>2002</option>
<option value='2003'>2003</option>
<option value='2004'>2004</option>
<option value='2005'>2005</option>
<option value='2006'>2006</option>
<option value='2007'>2007</option>
<option value='2008'>2008</option>
<option value='2009'>2009</option>
<option value='2010'>2010</option>
<option value='2011'>2011</option>
<option value='2012'>2012</option>
<option value='2013'>2013</option>
<option value='2014'>2014</option>
<option value='2015'>2015</option>
<option value='2016'>2016</option>
<select name='month'>
<option value='01'>January</option>
<option value='02'>February</option>
<option value='03'>March</option>
<option value='04'>April</option>
<option value='05'>May</option>
<option value='06'>June</option>
<option value='07'>July</option>
<option value='08'>August</option>
<option value='09'>September</option>
<option value='10'>October</option>
<option value='11'>November</option>
<option value='12'>December</option>
<select name='day'>
<option value='01'>1</option>
<option value='02'>2</option>
<option value='03'>3</option>
<option value='04'>4</option>
<option value='05'>5</option>
<option value='06'>6</option>
<option value='07'>7</option>
<option value='08'>8</option>
<option value='09'>9</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
<option value='13'>13</option>
<option value='14'>14</option>
<option value='15'>15</option>
<option value='16'>16</option>
<option value='17'>17</option>
<option value='18'>18</option>
<option value='19'>19</option>
<option value='20'>20</option>
<option value='21'>21</option>
<option value='22'>22</option>
<option value='23'>23</option>
<option value='24'>24</option>
<option value='25'>25</option>
<option value='26'>26</option>
<option value='27'>27</option>
<option value='28'>28</option>
<option value='29'>29</option>
<option value='30'>30</option>
<option value='31'>31</option>
<td><input type="text" maxlength='100' name="designation" id="designation" required></td>
<td><input type="text" maxlength='100' name="department" id="department" required></td>
<td><input type="file" maxlength='100' name="image" required></td>
<input type="submit" name="login" value="Register Yourself">
//create a connection
$conn = mysqli_connect('localhost', 'root', '', 'attendance');

//on the click of submit button
if (isset($_POST['login'])) {

//capture the $_POST values
$name = $_POST['empname'];
$name = trim($name);
$email = $_POST['emailid'];
$email = trim($email);
$userid = $_POST['userid'];
$userid = trim($userid);
$pwd = $_POST['pwd'];
$pwd = trim($pwd);
$desg = $_POST['designation'];
$desg = trim($desg);
$dept = $_POST['department'];
$dept = trim($dept);
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$date = $year.$month.$day;

//display a message if there is a blank entry for email
if ($email=="") {
echo "Please enter a valid email id";

//display a message if there is a blank entry for userid
if ($userid=="") {
echo "Please enter a valid User Id";

//check if the email id exists
$sql_check_email = "select * from employee where emp_email='$email';";

mysqli_query($conn, $sql_check_email);

$aff_email = mysqli_affected_rows($conn);

// if email id exists ..display message
if ($aff_email==1) {

$msgemail = "The email id exists";
echo $msgemail;

//display error message if there is an error
} else if ($aff_email>1) {

$msgemail = "There are multiple employees with the same email";
echo $msgemail;

//display message if there is an error firing the query
} else if ($aff_email<0) {

echo "There is an error ..Try again";


//check if the user id exists
$sql_check_userid = "select * from employee_login where emp_uid='$userid';";

mysqli_query($conn, $sql_check_userid);

$aff_userid = mysqli_affected_rows($conn);

if ($aff_userid==1) {

$umsg = "User id already exist";

echo $umsg;

//display error message if there is an error when the query is fired
} else if ($aff_userid<0) {

echo "There is an error ..Try again";


//if neither the user id nor the email id exist, upload image and do the insert
if ($aff_userid==0 && $aff_email==0) {

$target_path = "images/";
$target_file = $target_path . basename ($_FILES['image']['name']);

//if the image is moved to the images folder , do the insert
if (move_uploaded_file($_FILES['image']['tmp_name'], $target_file)) {

$image = basename($_FILES['image']['name']);

$sql_ins = "INSERT INTO temporary_employee(emp_uid,emp_pwd,

mysqli_query($conn, $sql_ins);

$aff_insert = mysqli_affected_rows($conn);

//display success message if insert is successfull
if ($aff_insert==1) {

echo "You have successfully registered ...awaiting approval by admin";

//display message if there were no insert
} else if ($aff_insert==0) {

echo "The registration has failed ..Try again";

//diplay error message if there was an error while firing the insert query
} else if ($aff_insert<0) {

echo "There was an error ..Try again";





Answer Source

While using Image Uploading in the form you have to use the enctype in the form attribute.

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

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download