I have the code below working fine but the challenge is this, when the user decides not to update the image field, the update statement clears out whatever I already have stored in the database. How do I write code that will leave the image update out if no image was set for update from my form and still work if image is set for update? The code below:
if(isset($_POST['button']) && $_POST['button'] == 'update')
{
if (!is_uploaded_file($_FILES['logo']['tmp_name']))
{
$uploadError[] = 'There was no file uploaded!';
}
$uploadfile = filter_var($_FILES['logo']['tmp_name'], FILTER_SANITIZE_STRING);
$uploadname = filter_var($_FILES['logo']['name'], FILTER_SANITIZE_STRING);
$uploadtype = filter_var($_FILES['logo']['type'], FILTER_SANITIZE_STRING);
$uploadsize = filter_var($_FILES['logo']['size'], FILTER_SANITIZE_STRING);
if(!empty($uploadfile))
{
$uploaddata = file_get_contents($uploadfile);
}
else{
$uploaddata = '';
}
$maxsize = 102400;
if($uploadsize > $maxsize) {
$error[] = "The file you're trying to upload is bigger than approved 1 MB.";
}
$conn = DatabaseManager::getConnection();
try
{
$sql = "UPDATE biz SET website = :website, officeaddress = :officeaddress, openingdays = :openingdays, contactperson = :contactperson,
whypatronize = :whypatronize, filename = :filename, mimetype = :mimetype, filedata = :filedata WHERE username = :username";
$s = $conn->prepare($sql);
$s->bindValue(':website', $website);
$s->bindValue(':officeaddress', $officeaddress);
$s->bindValue(':openingdays', $openingdays);
$s->bindValue(':contactperson', $contactperson);
$s->bindValue(':whypatronize', $whypatronize);
$s->bindValue(':filename', $uploadname);
$s->bindValue(':mimetype', $uploadtype);
$s->bindValue(':filedata', $uploaddata);
$s->bindValue(':username', $username);
$s->execute();
$conn = null;
}
catch (PDOException $e)
{
$error[] = "There was an error while updating your information, Please try again later";
}
if($s->rowCount() == 1)
{
$_SESSION['update'] = "Your information was successfully updated.";
header('Location: ../status/');
exit();
}
else
{
$error[] = 'Unable to perform your request now, Please try again later.';
}
}
Thanks a lot StarLion, your example worked, I just changed all the isset to !empty. Dormilich, I’m grateful to you too. I will check what Doctrine DBAL is about.
Please help me check this too. It works fine if I upload image but if I omit image, it doesn’t work and show me this error message: Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty in… What could be wrong?
if (isset($_POST['button']) && $_POST['button'] == 'add')
{
// Bail out if the file isn't really an upload
if (!is_uploaded_file($_FILES['logo']['tmp_name']))
{
$error[] = 'There was no file uploaded!';
}
$uploadfile = filter_var($_FILES['logo']['tmp_name'], FILTER_SANITIZE_STRING);
$uploadname = filter_var($_FILES['logo']['name'], FILTER_SANITIZE_STRING);
$uploadtype = filter_var($_FILES['logo']['type'], FILTER_SANITIZE_STRING);
$uploadsize = filter_var($_FILES['logo']['size'], FILTER_SANITIZE_STRING);
$uploaddata = file_get_contents($uploadfile);
$maxsize = 102400;
if($uploadsize > $maxsize) {
$error[] = "The file you're trying to upload is bigger than approved 1 MB.";
}
if(empty($missing) && empty($error))
{
$conn = DatabaseManager::getConnection();
try
{
$sql = "INSERT INTO jobseekers (website, filename, mimetype, filedata, userName)
VALUES (:website, :filename, :mimetype, :filedata, :userName)";
$s = $conn->prepare($sql);
$s->bindValue(':website', $website);
$s->bindValue(':filename', $uploadname);
$s->bindValue(':mimetype', $uploadtype);
$s->bindValue(':filedata', $uploaddata);
$s->bindValue(':username', $username);
$s->execute();
$conn = null;
}
catch (PDOException $e)
{
$error[] = 'There is a problem, Please try again later.';
}
if($s->rowCount() == 1)
{
$formProcessed = true;
}
else
{
$error[] = "Unable to add new job application to the database.";
}
}
}
if ($formProcessed) {
$_SESSION['success'] = 'The info has been successfully added to the database.';
header('Location: ../status/');
exit();
}
Well, your ‘bail out’ doesnt actually bail out of anything. So… yes. This will happen.
I’m going to say again that the rest of the page you’re showing us looks like the entire thing is JUST an image-upload script, so i’m not sure why you’re coding around the case of not having an image to upload.
Like I said, the code works well if I upload anything. The code is a bit longer than this, so I trimmed it down so that the upload issue can be sorted out. I want a situation where if the user decides not to upload anything, other part of the code executes. Thanks in advance.