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>