Selective Update Using PHP PDO

Good day house:

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.';
        } 
     
}

you have to run separate update statements for those two cases.

I have tried it, it did not work. How do I go by it and which one should come first?

neither. you need to make a separate statement for each case since they have different numbers of parameters.

Not… QUITE true.

You can do it all in one block of code; while the queries are technically different and ‘seperate’.

I… cant actually see where in OP’s code it references an image, the code appears to be an entirely file-upload-dependant form.

But, in basic;

$sql = "UPDATE atable SET datemodified = NOW(), aname = :aname, bname = :bname".((isset($acheckedvalue)) ? ", cname = :cname" : "").", dname = :dname";
$s = $conn->prepare($sql);
$s->bindValue(':aname',$avalue);
$s->bindValue(':bname',$bvalue);
if(isset($acheckedvalue)) { $s->bindValue(':cname',$cvalue); }
$s->bindValue(':dname',$dvalue);

Thanks Dormilich and StarLion. I will try out StarLion’s prototype and get back to the house.

if you used a library such as Doctrine DBAL, that issue would become easy to solve:

// using DBAL
$fields = array(
    'field_1' => 'value_1',
    'field_2' => 'value_2'
);
if ($another_field) {
    $fields['another_field'] = $another_field;
}
$dbal->update('table_name', $fields);

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.

Wrap the whole upload-image section of the code in a great big IF that checks to see if $_FILES[‘logo’] is set.

Thanks man. I’ve overcome the nightmare. I made some modifications to my code. Everything is perfect now.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.