-3
Warning: Undefined array key "image" in C:\xampp\htdocs\blogg\admin\include\edit_post.php on line 33

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\blogg\admin\include\edit_post.php on line 33

Warning: Undefined array key "image" in C:\xampp\htdocs\blogg\admin\include\edit_post.php on line 34

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\blogg\admin\include\edit_post.php on line 34

[code snippet 1](https://i.stack.imgur.com/IfWNw.png)

I was expecting to be able to update the image but that is not working out. Also, in the update, there is no where to click to choose a different image.

code snippet 2

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 1
    Please don't post picture of code – Chris Haas Aug 09 '23 at 13:17
  • Please read [ask] and how to make a [mre] of your issue, and then [edit] your post. As you're new please also take the [tour] to ensure you understand how stackoverflow works. Thanks – ADyson Aug 09 '23 at 13:22
  • 3
    The "image" key does not exist inside `$_FILES`. You should check what `$_FILES` actually contains. – Lajos Arpad Aug 09 '23 at 13:23

1 Answers1

2

You can never trust an array index to exist (no mechanism in PHP can enforce that). A general good practice when accessing an array is to either :

  • Check if it exists beforehand
if (isset($_FILES['image'])) {
  $post_image = $_FILES['image'];
} else {
  // handle error
}
  • Provide a fallback value when it doesn't
$post_image = $_FILES['image'] ?? '';

In your case, the index certainly does not exist because you submitted the form without uploading a file in the "image" field. If you consider the file to be mandatory, the first snippet is better. If it's not, the second one is shorter and easier.

Halibut
  • 188
  • 9