错误:未定义索引:图像($ _FILES ['图像'] ['名称'])

时间:2016-08-30 04:57:52

标签: php html mysql

我有一个简单的注册表单,其中我接受来自用户的包含图像的输入,并将值插入表:temporary_employees表。在我的代码中,我检查用户输入的电子邮件ID和用户ID是否已经存在,如果它们不存在,我继续执行插入后将图像移动到名为' images'的文件夹中。 。在运行代码时,我收到错误未定义索引:图像,在我有以下代码的行上:

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

最有趣的是同一行代码在另一个php文件中运行得非常好。我在html表单中为输入指定了相同的名称。。这怎么可能 ?任何帮助将不胜感激。

这是我的代码:     

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

        }


    }

}

?>

2 个答案:

答案 0 :(得分:1)

更改

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

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

再试一次。

enctype属性指定在将表单数据提交给服务器时应如何编码表单数据。

答案 1 :(得分:1)

  

在表单中使用图像上传时,您必须在表单属性中使用enctype。

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

enter image description here