I have a form which asks for an image uploaded, title, description etc.
An error appears if no image is selected like this:
if ($_FILES["target_file"]==''){
echo 'Please upload a suitable photograph!<br>';
}
But if i select an image but don't fill in the rest of the form, it will upload it to my database.
I've tried something like this:
if (!empty($_FILES['target_file']) && (empty ($_POST['photo-title'])) && (empty ($_POST['photo-name'])) && (empty ($_POST['uploaded'])) && (empty ($_POST['genreID'])) && (empty ($_POST['description'])))
{
echo 'Please select a photograph!<br>';
}
But this doesn't work.
Any one any ideas on how i can make it display an error after selecting an image but not filling the rest of the form?
Try this:
if(!isset($_FILES['target_file']) || !is_uploaded_file($_FILES['target_file']['tmp_name'])){
echo 'Please upload an image!<br>';
} else if(empty($_POST['photo-title']) || empty($_POST['photo-name']) || empty($_POST['uploaded']) || empty($_POST['genreID']) || empty($_POST['description'])){
echo 'Please fill in all the input fields!<br>';
} else {
//upload everything
}
In your code, you used &&
, which means that the error would only echo if all the fields were empty. Also, the exclamation mark in front of empty($_FILES['target_file'])
means that you will get an error if there is a file, and no error when there isn't.
Also, it's not necessary to enclose every empty(/*post field*/)
part in brackets