-2

I am making a user profile having the functionality of changing profile pictures and adding/editing bio. I am taking the user input file (pic) and saving it on the database. However, a database update is successful but I am unable to retrieve the image. Same with the bio, the update is successful even it is changing and updating on site but on page load it is gone.

this code is fetching data shown on profile
    <?php

$con = mysqli_connect('localhost','root','','juwsp') ;
$result = mysqli_query($con,"SELECT *  FROM signup WHERE StudentId='" . $_SESSION["StudentId"] . "' and Password = '". $_SESSION["Password"]."'");

while($row = mysqli_fetch_array($result)){
    $FirstName= $row['FirstName'];
    $LastName= $row['LastName'];
    $StudentId = $row['StudentId'];
    $Email= $row['Email'];
    $Password = $row['Password'];
    $Department = $row['Department'];
    $Batch = $row['Batch'];
    $Faculty= $row['Faculty'];
    $Bio= $row['Bio'];
    $ProfilePic=$row['ProfilePic'];
}
  
mysqli_close($con);
?>

Code for profile pic selection and database updation

<form  method="post" enctype="multipart/form-data">

  <div class="profilecard  file-upload text-center"  >  
  <img  src="http://ssl.gstatic.com/accounts/ui/avatar_2x.png" class="avatar img-circle img-thumbnail" alt="avatar">

</div>
  

        <br><br>
       
      </div></hr>
      
     
      <input type="file" class="text-center center-block file-upload" id="profileImage" name="profileImage" > 
      <button type="submit" name="save_profile" class="btn btn-primary btn-block">Save User</button>
</form>    

script for bio updation on click and file upload

<script>
$(document).ready(function() {

  $("#Bio").click(function () {
   event.preventDefault();

<?php
$conn = mysqli_connect("localhost", "root", "", "juwsp");
  
    $Bio= $_POST['addbio'];
    $sql="UPDATE signup SET Bio='$Bio' WHERE StudentId='" . $_SESSION["StudentId"] . "'  ";
    $edit = mysqli_query($conn,$sql);
  
      
   ?>

});
  var readURL = function(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('.avatar').attr('src', e.target.result);
            }
    
            reader.readAsDataURL(input.files[0]);
        }
    }
    

    $(".file-upload").on('change', function(){
        readURL(this);
    });
   
});
</script>

Screenshots of profile

Code for uploading image file

<?php
  $msg = "";
  $msg_class = "";
  $conn = mysqli_connect("localhost", "root", "", "juwsp");
  if (isset($_POST['save_profile'])) {

   
    // for the database
    $profileImageName =  $_FILES["profileImage"]["name"];
    // For image upload
    $target_dir = "images/";
    $target_file = $target_dir . basename($profileImageName);
    // VALIDATION
    // validate image size. Size is calculated in Bytes
    if($_FILES['profileImage']['size'] > 200000) {
      $msg = "Image size should not be greated than 200Kb";
      $msg_class = "alert-danger";
    }
    // check if file exists
    if(file_exists($target_file)) {
      $msg = "File already exists";
      $msg_class = "alert-danger";
    }
    // Upload image only if no errors
    if (empty($error)) {
      if(move_uploaded_file($_FILES["profileImage"]["tmp_name"], $target_file)) {
        $sql = "UPDATE signup SET ProfilePic='$profileImageName' WHERE StudentId='" . $_SESSION["StudentId"] . "'";
        (mysqli_query($conn, $sql));
        
            

            }


        } 
        
        else {
           
        
        }
      } 
      
      else {
         
      }
    
  
?>
  • $_POST['addbio'] array is going to be empty, var_dump $_POST and see what data you need out of it. – Trukken Jun 29 '21 at 11:13
  • "I am unable to retrieve the image" - what does that mean? Also, be warned that your queries are widely open for SQL injection. Please have a look at prepared statements – Nico Haase Jun 29 '21 at 11:22
  • Retrieve meaning that the image uploads in db every time but i am unable to fetch it on page even the other data is fetched from the same query – Wajiha's World Jun 29 '21 at 11:24
  • 1
    What do you mean by "the image uploads in db every time"? You haven't shown any code that handles file uploads of any kind – Nico Haase Jun 29 '21 at 11:24
  • I forgot to add that. Now I have edited the question with that code – Wajiha's World Jun 29 '21 at 12:02
  • And what **exactly** is not working? Where's the code to "retrieve" the image? What happens when you run that code? – Nico Haase Jun 29 '21 at 12:03
  • when I run the code the image gets save i the database whereas it is not uploaded – Wajiha's World Jun 29 '21 at 12:04
  • $result = mysqli_query($con,"SELECT * FROM signup WHERE StudentId='" . $_SESSION["StudentId"] . "' and Password = '". $_SESSION["Password"]."'"); while($row = mysqli_fetch_array($result)){ $FirstName= $row['FirstName']; $LastName= $row['LastName']; $StudentId = $row['StudentId']; $Email= $row['Email']; $Password = $row['Password']; $Department = $row['Department']; $Batch = $row['Batch']; $Faculty= $row['Faculty']; $Bio= $row['Bio']; $ProfilePic=$row['ProfilePic']; } – Wajiha's World Jun 29 '21 at 12:05
  • I am saving pic in ProfilePic column and then callit in image source () – Wajiha's World Jun 29 '21 at 12:06
  • Please add all clarification to your question by editing it. Also, please add your attempts to resolve this problem to your question – Nico Haase Jun 29 '21 at 12:08

1 Answers1

1

Looking at your code you don't actually do anything with your image. In your JavaScript you read the contents of the file, but the result of this operation does not appear to be used anywhere.

I would suggest reading up on handling uploads in PHP: https://www.php.net/manual/en/features.file-upload.php

Timo
  • 158
  • 1
  • 7
  • But the file uploads everytime in database – Wajiha's World Jun 29 '21 at 11:22
  • I am having a hard time figuring out the flow of your application. So your form submits to the bottom script. That uploads the images and updates the table. What data is in the table after that script is finished? Looking at the code - did you have the bottom snippet in your question from the start? - you store the name of the file in the table. So if I upload a file called `my-profile.png` I expect the table to have exactly that content. You move the file to `./images`. So to show the image on the web you will need to add `images/` to the data in the database. – Timo Jun 30 '21 at 12:44