Hello everyone,

I am trying to finish my first form with an optional file upload input field. So far the form works very nicely but I am having problem with the validation part when someone fill-in a form but doesn't upload a file. I would like to be able to still save the info submited to mysql even if there is no picture uploaded. can anyone help?

<body>
<?php
//DB Connection//
include_once ("../include/connect.php");
include_once ("../include/appvar.php");
//constant to upload pictures//
//define('EMP_UPLOADPATH',"../image/employee");

//check to see if the form has been set//
  if (isset($_POST['submit'])) {
	//Variables for employee form//

$employerid= mysqli_real_escape_string($dbc,trim($_POST['employerid']));
$jobtitleid= mysqli_real_escape_string($dbc, trim($_POST['jobtitleid']));
$firstname= mysqli_real_escape_string($dbc, trim($_POST['firstname']));
$lastname= mysqli_real_escape_string($dbc, trim($_POST['lastname']));
$address= mysqli_real_escape_string($dbc, trim($_POST['address']));
$city= mysqli_real_escape_string($dbc, trim($_POST['city']));
$province= mysqli_real_escape_string($dbc, trim($_POST['province']));
$country= mysqli_real_escape_string($dbc, trim($_POST['country']));
$postalcode= mysqli_real_escape_string($dbc, trim($_POST['postalcode']));
$phone= mysqli_real_escape_string($dbc, trim($_POST['phone']));
$email= mysqli_real_escape_string($dbc, trim($_POST['email']));
$employeecomment = mysqli_real_escape_string($dbc, trim($_POST['employeecomment']));
$employeepic = mysqli_real_escape_string($dbc, trim($_FILES['employeepic']['name']));
$employeepic_type = $_FILES['employeepic']['type'];
$employeepic_size = $_FILES['employeepic']['size'];
  //Validate picture//


	if (empty($_FILES['employeepic']['name']) && empty($_FILES['employeepic']['tmp_name'])){
	echo '';
	}else{
		(($employeepic_type == 'image/gif') || ($employeepic_type == 'image/jpeg') || ($employeepic_type == 'image/jpg')
		|| ($employeepic_type == 'image/pjpeg') || ($employeepic_type == 'image/png')) && (($employeepic_size <= EMP_MAXFILESIZE) && 
			($_FILES['employeepic']['error'] == 0));
			// Move the file to the target upload folder
				$target = EMP_UPLOADPATH .$firstname.$employeepic ;
				move_uploaded_file($_FILES['employeepic']['tmp_name'], $target);

					if (empty($employerid) ||empty($firstname) ||empty($lastname)||empty($jobtitleid)) {
		echo 'all required fields are required, Dhuuhaa';

	}else{

					//query to populate employee form//
					$query = "INSERT INTO employee (employerid, jobtitleid, firstname, lastname, address, city, province, country, postalcode," .
					"phone, email, employeecomment, employeepic) VALUES ('$employerid', '$jobtitleid', '$firstname', '$lastname'," .
					" '$address', '$city', '$province', '$country', '$postalcode', '$phone', '$email','$employeecomment','$firstname$employeepic')";

					$result = mysqli_query($dbc, $query);

					mysqli_close($dbc);
			}



	}
}

?>
<div class="gridContainer clearfix">
  <header class="fluid header"><h1>Shannon Falls Retirement Residence Database</h1></header>
  	<nav class="fluid nav">This is the content for Layout Nav Tag "nav"</nav>
    	<aside class="fluid aside">
        	<div class="fluid" id="hero"><?php if (isset($_POST['submit'])){	echo ' <h1>'. $firstname . ' ' .$lastname.'</h1>';	}?></div>
			<div class="fluid emp_pic_frame"> <?php if (isset($_POST['submit'])){
												echo '<img src="' . EMP_UPLOADPATH   .$firstname.$employeepic . '" alt="Score image" />'; 
												}?></div><hr>
         </aside>
    		<div class="fluid content">

				<h2>Employees Details</h2><br /><hr>

			  <form  enctype="multipart/form-data" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
              <input type="hidden" name"MAX_FILE_SIZE" value="<?php echo EMP_MAXFILESIZE; ?>" />
              <label for="employerid" class="label" >Employer:</label>                
              <input type="text" class="input" id="employerid" name="employerid" value="<?php if (!empty($employerid)) echo $employerid; ?>"/>

              <label for="jobtitleid" class="label" >Job Title:</label>                
              <input type="text" class="input"  id="jobtitleid" name="jobtitleid" value="<?php if (!empty($jobtitleid)) echo $jobtitleid; ?>"/>

              <label for="firstname" class="label" >First Name:</label>                
              <input type="text" class="input" id="firstname" name="firstname" value="<?php if (!empty($firstname)) echo $firstname; ?>"/>

              <label for="lastname" class="label" >Last Name:</label>                
              <input type="text" class="input" id="lastname" name="lastname" value="<?php if (!empty($lastname)) echo $lastname; ?>"/>

              <label for="address" class="label" >Address:</label>                
              <input type="text" class="input" id="address" name="address" value="<?php if (!empty($address)) echo $address; ?>"/>

              <label for="city" class="label" >City:</label>                
              <input type="text" class="input" id="city" name="city" value="<?php if (!empty($city)) echo $city; ?>"/>

              <label for="province" class="label" >Province:</label>                
              <input type="text" class="input" id="province" name="province" value="<?php if (!empty($province)) echo $province; ?>"/>

              <label for="country" class="label" >Country:</label>                
              <input type="text" class="input" id="country" name="country" value="<?php if (!empty($country)) echo $country; ?>"/>

              <label for="postalcode" class="label" >Postal Code:</label>                
              <input type="text" class="input" id="postalcode" name="postalcode" value="<?php if (!empty($postalcode)) echo $postalcode; ?>"/>

              <label for="phone" class="label" >Phone:</label>                
              <input type="text" class="input" id="phone" name="phone" value="<?php if (!empty($phone)) echo $phone; ?>"/>

              <label for="email" class="label" >Email:</label>
              <input type="text" class="input" id="email" name="email" value="<?php if (!empty($email)) echo $email; ?>"/><br /><br />

              <label id="employeecommentlabel" for="employeecomment">Employee Comment:</label><br />                
              <textarea name="employeecomment" id="employeecomment" value="<?php if (!empty($employeecomment)) echo $employeecomment; ?>"></textarea><br />  

              <label id="employeepiclabel"for="employeepic">Employee Picture:</label>
              <input type="file" name="employeepic" id="employeepic" /><hr>

              <input id="submit" type="submit" name="submit" value="Save"/>
        	</form>

    </div content>
    <footer class="fluid footer">This is the content for Layout Footer Tag "footer"</footer>
</div container clearfix>
</body>

    Check to see if [font=monospace]$_FILES['employeepic']['size'][/font] is zero or not. If it isn't set [font=monospace]$employeepic[/font] and related variables to whatever you want to be stored in that situation. Else, set them as you're doing.

    Also, don't hide all the insertion code behind the check for empty file names (if you don't want to validation on a certain field, don't do any validation on that field). And of course, check that a file exists before you try to move it around.

    What is the problem you say you're having?

      This is what I use in this case. I have this in the subroutine where I check all the post variables that have been sent by the form.

      // Move the file over.
      if($FILES['upload']['tmp_name']!=""){
      $extension = substr ($
      FILES['upload']['name'],-4);
      $filename ="cust".strtolower($id).strtolower($extension);
      if (move_uploaded_file($FILES['upload']['tmp_name'], $cnfig['datadir']."/".$filename)){
      $taxfile=$filename;
      list($width, $height, $type, $attr) = getimagesize($cnfig['datadir']."/".$taxfile);
      if($width>500){
      smart_resize_image( $filename,"500","0","true",".",$cnfig['datadir']);
      }
      }
      }else{
      $filename=$
      POST['photo'];
      }

      it basically checks to see if there is a file that has been uploaded.

      Sets the name for it. (I did this because people name pictures some very odd things sometimes and it doesn't work well with my code. You can never anticipate all the strange things people will call their files. So I rename them to something that works and I can identify it with where I'm putting it.)
      Re-sizes it if necessary
      then inserts the $filename I gave it into the table along with the rest of the post data. If they don't upload a file you can set the filename to NULL or just not include it in the insert. Oh you might notice that in the example it is setting $filename to a post variable if they didn't upload a pic. That is because this was an update so they can update the info but not change the photo. Works the same with a totally new form except you don't have a $_POST['photo'] to use if they don't upload the file.

        Write a Reply...