0

I have a simple registration form, in which I accept inputs from the user that 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) {
    session_start();
}

?>
<!DOCTYPE html>
<html>
<head>
<title>
Login form
</title>
</head>
<body>
<h3>Registration Form</h3>
<form action ="" method="POST">
<table align="center" cellpadding="10">
<tr>
<td>Name</td>
<td><input type="text" maxlength='100' name="empname" id="empname" required></td>
</tr>
<tr>
<td>Email Id</td>
<td><input type="text" maxlength='100' name="emailid" id="emailid" required>
</td>
</tr>
<tr>
<td>User Id</td>
<td><input type="text" maxlength='100' name="userid" id="userid" required ></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" maxlength='100' name="pwd" id="pwd" required ></td>
</tr>
<tr>
<td>Date of Birth</td>
<td>
<select name='year'>
<option value='2015'>2015</option>
<option value='2016'>2016</option>
</select>
<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>
</select>                   
<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>            
</select></td>
</tr>
<tr>
<td>Designation</td>
<td><input type="text" maxlength='100' name="designation" id="designation" required></td>
</tr>
<tr>
<td>Department</td>
<td><input type="text" maxlength='100' name="department" id="department" required></td>
</tr>
<tr>
<td>Image</td>
<td><input type="file" maxlength='100' name="image" required></td>
</tr>
<tr>
<td>
<input type="submit" name="login" value="Register Yourself">
</td>
</tr>
</table>
</form>
</body>
</html>
<?php                   
//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,
                        emp_name,emp_email,emp_dob,emp_designation,
                        emp_department,emp_image) 
                        VALUES('$userid','$pwd','$name','$email','$date',
                        '$desg','$dept','$image')";

            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";
            }   

        }


    }

}

?>
Sabyasachi Gupta
  • 153
  • 2
  • 13

2 Answers2

2

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">
</form>

enter image description here

Naresh Kumar P
  • 4,127
  • 2
  • 16
  • 33
  • Since you have to encrypt the images and then only you have to post the image using HTML form... – Naresh Kumar P Aug 30 '16 at 05:00
  • @Naresh..thks ..it works fine .. I also have a question here : when i changed my code to :
    , it was still giving me the same error . It was only after i changed my code to
    , that the insertion was successfull . Without the image upload part , form action ="" , works perfectly fine . Can you tell me why this is so ?
    – Sabyasachi Gupta Aug 30 '16 at 05:22
  • Have you written the form uploader code in `test-reg.php` page right. – Naresh Kumar P Aug 30 '16 at 05:25
  • yes Naresh..thats right . my form and form uploader code is in the same file ..test-reg.php . But , my question remains ; when i work without a file upload control , and my form upload code is in the same file, form action="" works perfectly fine . But when i use a file upload control and my form upload code is in the same file, form action="" gives an error . Can you tell me why it is so ? – Sabyasachi Gupta Aug 30 '16 at 05:36
  • What Error are you getting @SabyasachiGupta. when you are using file control in the form with action="". – Naresh Kumar P Aug 30 '16 at 05:38
  • If you are using file uploder you have to specify the file in which the code is written since the file uploder will search for the `$_FILES` syntax and hence without specifying the path the data are not received to this function. If you use without the file control all the other data will be inserted correclty – Naresh Kumar P Aug 30 '16 at 05:46
  • i am getting the same error posted in my question "undefined index image" . – Sabyasachi Gupta Aug 30 '16 at 05:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122157/discussion-between-naresh-kumar-p-and-sabyasachi-gupta). – Naresh Kumar P Aug 30 '16 at 05:51
  • thks ..Naresh for all your clarifications – Sabyasachi Gupta Aug 30 '16 at 05:52
1

Change

<form action ="" method="POST">

to

<form enctype="multipart/form-data">

And try again.

The enctype attribute specifies how the form-data should be encoded when submitting it to the server.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59