Home > Software engineering >  Undefined Array Key "image"
Undefined Array Key "image"

Time:05-16

When i'm uploading image in my form it gives me Undefined Array Key "image" i'm giving it the EncType and right name but i don't know where is the problem

MySQL insert code

if ($_SERVER['REQUEST_METHOD'] == "POST") {
  if (isset($_POST['send'])) {
    $First_Name = $_POST['First_Name'];
    $Last_Name = $_POST['Last_Name'];
    $Email = $_POST['Email'];
    $Phone = $_POST['Phone'];
    $Password = sha1($_POST['Password']);
    $country = $_POST['country'];
     $image = $_POST['image'];

    if (isset($_POST['image'])) {
      $imageName = $_FILES['image']['name'];
      $imageType = $_FILES['image']['type'];
      $imageTmp = $_FILES['image']['tmp_name'];
      move_uploaded_file($imageTmp, "images/", $image);
    }
    $sql = "INSERT INTO users(first_name,last_name,email,phone,password,country,image) VALUES ('$First_Name','$Last_Name','$Email','$Phone','$Password','$country','$image')";
  }
}

HTML Code

    <form method="POST" action="index.php" enctype="multipart/form-data">
      
           <div >
        <label for="exampleInputPassword2">image</label>
        <input type="file"  id="exampleInputPassword2" name="image">
      </div>


    </form>

CodePudding user response:

please check my below code. it will be helpful for you.

if ($_SERVER['REQUEST_METHOD'] == "POST") {
  if (isset($_POST['send'])) {
    $First_Name = $_POST['First_Name'];
    $Last_Name = $_POST['Last_Name'];
    $Email = $_POST['Email'];
    $Phone = $_POST['Phone'];
    $Password = sha1($_POST['Password']);
    $country = $_POST['country'];
    $image = $_FILES['image']['name'];
    $folder = "images/".$image;


    if (isset($image)) {
      $imageName = $_FILES['image']['name'];
      $imageType = $_FILES['image']['type'];
      $imageTmp = $_FILES['image']['tmp_name'];
      move_uploaded_file($imageTmp,$folder);

    }


$sql = "INSERT INTO users(first_name,last_name,email,phone,password,country,image) VALUES ('$First_Name','$Last_Name','$Email','$Phone','$Password','$country','$image')";`
      }
    }
  • Related