How to keep image input on if other validation fails?

Hi I have created a form that allow user to enter 2 input and an image, I’ve valided the fields using if empty, the problem is if i fill the image input and leave the other empty it gives the validation error but also removes the image field that I have already placed on the image input,
I’ve tried to keep like below but still always empty the image input.

<INPUT type="file" name="userfile" value="<?php echo $userfile;?>">

also tried
<INPUT type=“file” name=“userfile” value=“<?php if (isset($userfile)) { echo $userfile;} ?>”>

here my full script

<?php
     include "connection.php"; // find file (connection.php)
 $message = ""; // this variable starts empty then bellow it will be given variables depend on the error message
 $error_name = "";
 $error_dogsize = "";
if (isset ($_POST["submit"])) { // if post has been set/clicked run the code below

$first_name = mysql_real_escape_string($_POST['first_name']);
$dogsize = mysql_real_escape_string($_POST['dogsize']);

// start validate name
  if (empty($first_name)) // if name is empty
	{
		$error_name ='Please enter name';
	}	
	 else if(!preg_match("#^[-A-Za-z' ]*$#",$first_name)) { // name can only contain letter
		$error_name ='Name must contain letter only';
	}
	
	  if (empty($dogsize)) // if dogSize is empty
	{
		$error_dogsize ='Please select size of the dog';
	}	

	
// end validation name

/*START IMAGE VALIDATION */
   // Configuration - Your Options
      $name = $_FILES['userfile']['name']; // get the name of the file
      $type = $_FILES['userfile']['type']; // get the type of the file
      $size = $_FILES['userfile']['size']; // get the size of the file

      $allowed = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
      $max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
      $upload_path = './images/'; // The place the files will be uploaded to (currently a 'files' directory).
   		
   		$ext = substr($name, strpos($name,'.'), strlen($name)-1); // Get the extension from the filename.

     $fileType = in_array($ext, $allowed); // add the files type inside array filetype
	
   // Check if the filetype is allowed, if not DIE and inform the user.
   if(!$fileType) :
      $message = '<br />The file you attempted to upload is not allowed.';
   endif;

   // Now check the filesize, if it is too large then DIE and inform the user.
    if($size > $max_filesize) :
       $message = 'The file you attempted to upload is too large.';
   endif;

     $upload = is_writable($upload_path);
   // Check if we can upload to the specified path, if not DIE and inform the user.
   if(!$upload) :
        $message = 'You cannot upload to the specified directory, please CHMOD it to 777.';
   endif;
	
    $filename = time().$ext; // this will give the file current time so avoid files having the same name

   // if file type is less than maximum amount and file is uploaded and the error name error variable is empty run the code
	if($fileType && $size < $max_filesize && $upload && $error_name == "" && $error_dogsize == "")
		{
  			// Upload the file to your specified path.
			if(move_uploaded_file($_FILES["userfile"]["tmp_name"],$upload_path . $filename))
/* END IMAGE VALIDATION */
				{
   // insert value into the database
					$query = "INSERT INTO animals (id, first_name, dogsize,  image)
                   			VALUES ('', '$first_name', '$dogsize',  '$filename')";
                    mysql_query($query) or
                    die (mysql_error());

					echo time(). ' Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>';
	 				$current_url = (empty($_SERVER['HTTPS']) ? "http://" : "https://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
					header ('Location: ' . $current_url);
					exit ();
			 		// It worked.
				}
		}
  //    else
   //      echo 'There was an error during the file upload.  Please try again.'; // It failed :(.
//http://stackoverflow.com/questions/2666882/how-to-avoid-resending-data-on-refresh-in-php

//scape string http://stackoverflow.com/questions/13034868/form-to-insert-data-in-database-works-but-does-not-show-success-page
		
}

?>

<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
<h2>Upload an image</h2>
<?php echo $error_name .'<br />';  ?>
What is your name?<br>
<input type="text" name="first_name" maxlength="50" value="<?php if (isset($first_name)) { echo $first_name;} ?>" /><br />
<br />
<?php echo $error_dogsize .'<br />';  ?>

Select dog size:
<select name="dogsize" id="dogsize">
  <option value="">Select</option>
  <option value="large">Large</option>
  <option value="medium">Medium</option>
  <option value="small">Small</option>
</select>
<br />
<?php echo $message.'<br />';  ?>
Upload an image: <br /><INPUT type="file" name="userfile" value="<?php if (isset($userfile)) { echo $userfile;} ?>">
<br />
<input type="submit" name="submit" value="Submit">
</form>

(isset($userfile)) { echo $userfile;}

I don’t see $userfile set as a variable in your code anywhere.

Hi I just thought because thats the name of the image input
do you know which variable I need so that when validates doesnt remove the image input so that the user doesnt have to add the image again everytime the other fails

you can see the form here if you add add an image but leave the other fields empty it will remove the image
http://adoptadog.eurico.co.uk/index.php

That’s not enough. You need to store the user input for that name as a variable called $userfile. What do you actually want to be displayed there if the form fails? The name? Perhaps you should just echo $name. (I don’t know enough PHP to know if the other code you have will work anyhow, so I’ll leave that to someone else.)

well what I want is to retain the detail when upload the image like when you upload the image the upload field will contain the above
C:\Users\\Desktop\ ick bullets.jpg

but if other fields fail it automaticaly removes that field so the user need to get the image again I want to retain that on the upload field.

It’s a complicated manual process.

Basically you want to upload the file (move from the temp directory that PHP originally uploads to) to a publicly accessible temp folder. Then rewrite the form to display the image (or just the file name) and have a hidden field that has the image path. On a new form submission, you would check for this hidden field and use it if they didn’t upload a new image.