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>