0
session_start(); 
$_SESSION['id'] = '$id';
$_SESSION['name'] = '$name';
$_SESSION['phone'] = '$phone';
$_SESSION['email'] = '$email';
$_SESSION['image'] = '$image';
$_SESSION['error'] = '$error';

<form id="formm" action="" method="post">
<strong>Image: *</strong> <input type="file" src=images id="imageUpload" name="imageUpload"value="<?php echo $image; ?>"/>

add-student.php

$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$image = $_POST['image'];
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["imageUpload"]["name"]). " has been uploaded.";
}

I get an error message:

Undefined index: imageUpload

What am I doing wrong?

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
Yossi Bondasd
  • 147
  • 3
  • 12
  • 1
    It makes no sense to put `value="..."` in a `file` input. The user has to select the file. – Barmar May 23 '17 at 18:00
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Rasclatt May 23 '17 at 18:00
  • Your form doesn't have the proper `enctype`, needs `enctype="multipart/form-data"` – Rasclatt May 23 '17 at 18:03
  • 1
    Variables **doesn't expand inside single quotes** , use double quotes, i.e.: `....= "$id" ` or simply `....= $id;` – Pedro Lobito May 23 '17 at 18:06
  • code gets stuck here: $uploadfile = $uploaddir . basename... it says undefined index imageUpload. I tried to fix everything you wrote.. – Yossi Bondasd May 23 '17 at 18:36

2 Answers2

0
<input type="file" src=images id="imageUpload" name="imageUpload"value="<?php echo $image; ?>"/>

shouldn't be :
<form ....... enctype="multipart/form-data"> <input type="file" src="path/to/images" id="imageUpload" name="imageUpload" /> without the value
another thing, I see that you are using a variable between '', It will be token as a string and will not have the value of the variable here :

$_SESSION['id'] = '$id';
$_SESSION['name'] = '$name';
$_SESSION['phone'] = '$phone';
$_SESSION['email'] = '$email';
$_SESSION['image'] = '$image';
$_SESSION['error'] = '$error';

shouldn't be :

$_SESSION['id'] = $id;
$_SESSION['name'] = $name;
$_SESSION['phone'] = $phone;
$_SESSION['email'] = $email;
$_SESSION['image'] = $image;
$_SESSION['error'] = $error;
Omis Brown
  • 199
  • 2
  • 16
0

First, make sure you have file_uploads = On enabled in your php.ini configuration

Then fix your form code, it should look like this:

<form action="add-student.php" id="formm" method="post" enctype="multipart/form-data">
    <strong>Select image to upload:</strong>
    <input type="file" name="imageUpload" id="imageUpload" value="<?php echo $image; ?>">
    <input type="submit" value="Upload Image" name="submit">
</form>

That will make your file be submitted back to the php file.

I hope it helps :)

ajimix
  • 974
  • 7
  • 16
  • code gets stuck here: $uploadfile = $uploaddir . basename... it says undefined index imageUpload. I tried to fix everything you wrote.. – Yossi Bondasd May 23 '17 at 18:38
  • So if you followed my steps (and you know for sure that file_uploads is On and applied in your php configuration), can you do a `var_dump($_FILES);` in your add-student.php file and see what you get? – ajimix May 23 '17 at 18:42
  • array. i see in mysqli undefined under image. i need to see the full name of the the file. thanks for your efforts! – Yossi Bondasd May 23 '17 at 19:02