0

I have created a profile form for users to update their information. Within this form, the user should be able to update their profile picture. However, at this point in time, it is making me update the picture each time or the image (name) is removed from the DB. I would like to have it set up to where if the user does not update the photo the old image that is already set in the DB is used.

Code has not been refactored.

I have tried several approaches to get this to work. I have attempted to use if(isset($_FILES['profile_image'])), I have tried other if statements with no success.

I did not add all of the code to try to keep it cleaner for you. I apologize if I have left anything out.

PHP

if(isset($_POST['update_profile'])){

    if(isset($_FILES['profile_image']['name'])){
        $profile_image_temp = $_FILES['profile_image']['tmp_name'];
        $profile_image = $_FILES['profile_image']['name'];

        move_uploaded_file($profile_image_temp, 
"../includes/profile_images/$profile_image");
    } elseif (!isset($_FILES['profile_image']['name'])){
        $query = "SELECT * FROM profiles WHERE profile_id = $profile_id";
        $complete_query = mysqli_query($connection, $query);

        while ($row = mysqli_fetch_array($complete_query)){
            $profile_image = $row['profile_image'];
        }
    }

$update_profile = "UPDATE profiles SET profile_image = '{$profile_image}' 
WHERE profile_id = $profile_id";

HTML

<div class="contact-clean col-md-5" style="margin:0 auto; padding-top: 
50px;">
<h2 class="text-center">Update Profile</h2>
<p class="text-center"><small>Fields with an asterisk are required 
</small></p>
<form method="post" action="#" enctype="multipart/form-data">

<div class="form-group">
    <label for="profile_image">Profile Image * </label><input class="form- 
control" type="file" name="profile_image" id="profile_image" value="<?php 
echo $profile_image ?>">
</div>
<button  style="margin-top: 20px; margin-bottom: 300px;"" class="btn btn- 
primary btn-lg" type="submit" name="update_profile" 
id="update_profile">Update Profile</button>
</form>
</div>

Expected: If the user does not add an image their old image will still be there for them to use.

Actual Results: If the user does not add an image during the update the image is removed from the DB.

halfer
  • 19,824
  • 17
  • 99
  • 186
Kirby
  • 1
  • 1
  • Btw. do use prepared statements to prevent SQL injection! – Pinke Helga Mar 30 '19 at 04:20
  • Further more see error logs or turn on error outputs on development system including notices. – Pinke Helga Mar 30 '19 at 04:23
  • By the way, You stated that you have reformatted my question? I don't see any changes. I am new here and new to PHP, do I have to look somewhere else for the updates? – Kirby Mar 30 '19 at 04:48
  • You can review edits side-by-side clicking on the link [edited ## mins ago](https://stackoverflow.com/posts/55428022/revisions) below the question. There you can switch to *markdown* to learn how SO markdown features are applied. In the reformatted code you should see that `UPDATE` is outside the `if/elseif` branch, but only inside the the `if(isset($_POST['update_profile'])){` block, i.e. each time the button is pressed regardless to the field contents. – Pinke Helga Mar 30 '19 at 04:55
  • Ah, reviewing deeper, I see you try to fetch some image from DB. Why? Why not just omit the update conditionally? – Pinke Helga Mar 30 '19 at 05:01
  • I do not see any `profile_image` but `service_image` in your HTML. Your issue seems to be just a copy'n'paste typo. – Pinke Helga Mar 30 '19 at 05:03
  • Sorry, I must have copied over the wrong html portion for the image. I tried to fetch the image because I was trying to at least give it what I wanted it to have so that it wouldnt be NULL. I have tried so many things over the past two days its drove me insane to be honest. To answer the other questions. I do user prepared statements to prevent SQL injection I just havent right now until I can get it working. I also use the only error handling that I am aware of mysqli_error. – Kirby Mar 30 '19 at 05:07
  • In general, very helpful for debugging is https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-information-in-different-environments/22662582#22662582 and you will get exceptions. I'ld do the update within `if(isset($_FILES['profile_image']['name'])){ ...` You should edit your question and insert the matching HTML snippet. – Pinke Helga Mar 30 '19 at 05:10
  • I will update the HTML with the correct code. However, looking at the side-by-side markdown it doesn't appear as though anything was changed (other than the spacing)!? – Kirby Mar 30 '19 at 05:25

0 Answers0