Am trying to insert data into a database table of which store references to my gallery images but when a user fails to insert data i like to return a customized error message but it keeps on showing this error(or notice).
Undefined index: gallery in C:\xampp\htdocs\unik\admin\add_gallery.php
Here is a sample of the code am using
if (isset($_POST['submit'])) {
$input['caption'] = escape($_POST['caption']);
if ($input['caption'] == '' || $_FILES['gallery']['error']) {
if ($input['caption'] == '') { $error['caption'] = "Image Caption required!"; }
if ($_FILES['gallery']['error']) { $error['gallery'] = $upload_errors[$_FILES['gallery']['error']]; }
$error['alert'] = "Please fill in all the required fields!";
include 'gallery_form.php';
} else {
$tmp_file = $_FILES['gallery']['tmp_name'];
$target_file = basename($_FILES['gallery']['name']);
if (file_exists($image_path.$target_file)) {
unlink($image_path.$target_file);
}
if (move_uploaded_file($tmp_file, $image_path.$target_file)) {
// continue here
} else {
$error['alert'] = "There was a problem with the file upload.";
$error['gallery'] = $upload_errors[$_FILES['gallery']['error']];
include 'gallery_form.php';
}
}
and here is the form code
<form action="" method="POST" autocomplete="off">
<?php if (isset($error['alert'])) { ?>
<div class="alert"><?php echo $error['alert']; ?></div>
<?php } ?>
<div>
<label for="gallery">Image*</label>
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
<input type="file" name="gallery" id="gallery"/>
<?php if (isset($error['gallery'])) { ?><div class="error"><?php echo $error['gallery']; ?></div><?php } ?>
<label for="caption">Image Caption</label>
<input type="text" name="caption" id="caption" placeholder="Caption" value="<?php if (isset($input['caption'])) { echo $input['caption']; } ?>" />
<?php if (isset($error['caption'])) { ?><div class="error"><?php echo $error['caption']; ?></div><?php } ?>
<p class="required">* Required</p>
<input type="submit" name="submit" id="submit" class="submit" value="Submit" />
</div>
</form>
The error is coming from here in the code
if ($_FILES['gallery']['error']) { $error['gallery'] = $upload_errors[$_FILES['gallery']['error']]; }
so how do i solve it.