title: How to add profile image to user php
body: <p>I want a logged in user to add a profile picture. No errors are shown, the picture is just not added to the folder where it should be. 
  I know I have to use prepared statements, I will. I just want to sort this problem out first.</p>
  
  <p>When the user has not changed the profile pic, the default picture displays perfectly. The file profile pic just wont upload to the folder.</p>
  
  <p>This is the page where you change the picture.</p>
  
  <pre><code>  &lt;?php
    session_start();
    include_once 'dbh.php';
  
    &lt;html&gt;
  
    &lt;body&gt;
    &lt;?php
  
     $sql = "SELECT * FROM user";
     $result = mysqli_query($conn, $sql);
     if (mysqli_num_rows($result) &gt; 0) {
       while ($row = mysqli_fetch_assoc($result)) {
        $id = $row['id'];
  
        $sqlImg = "SELECT * FROM profileimg WHERE userid='$id'";
        $resultImg = mysqli_query($conn, $sqlImg);
        while ($rowImg = mysqli_fetch_assoc($resultImg)) {
  
          echo "&lt;div&gt;";
  
            if ($rowImg['status'] == 0) {
              echo "&lt;img src='uploads/profile".$id.".jpg'&gt;";
            }
  
            else {
              echo "&lt;img src='uploads/male.jpg'&gt;";
            }
  
            echo "&lt;p&gt;".$row['username']."&lt;/p&gt;";
          echo "&lt;/div&gt;";
        }
      }
    }
    else {
      echo "There are no users!";
    }
  
  
    if (isset($_SESSION['id'])) {
      echo "You are logged in!";
  
      echo '&lt;form action="includes/upload.inc.php" method="post" 
      enctype="multipart/form-data"&gt;
        &lt;input type="file" name="file"&gt;
        &lt;button type="submit" name="submit"&gt;UPLOAD FILE&lt;/button&gt;
      &lt;/form&gt;';
    }
    else {
      echo "You are not logged in!";
  
    }
  ?&gt;
  </code></pre>
  
  <p>This is the php page for the upload</p>
  
  <pre><code>&lt;?php
  
  session_start();
  include_once 'dbh.php';
  $id = $_SESSION['id'];
  
  
  if (isset($_POST['submit'])) {
  
  $file = $_FILES['file'];
  $fileName = $file['name'];
  $fileType = $file['type'];
  $fileTempName = $file['tmp_name'];
  $fileError = $file['error'];
  $fileSize = $file['size'];
  $fileExt = explode('.', $fileName);
  $fileActualExt = strtolower(end($fileExt));
  $allowed = array("jpg", "jpeg", "png", "pdf");
  
  
  if (in_array($fileActualExt, $allowed)) {
  
    if ($fileError === 0) {
  
      if ($fileSize &lt; 500000) {
        //I now need to create a unique ID which we use to replace the name 
         of the uploaded file, before inserting it into our rootfolder
        //If I don't do this, we might end up overwriting the file if we 
         upload a file later with the same name
        //Here I use the user ID of the user to create the first part of the 
         image name
        $fileNameNew = "profile".$id.".".$fileActualExt;
        $fileDestination = 'uploads/'.$fileNameNew;
        move_uploaded_file($fileTmpName, $fileDestination);
  
        $sql = "UPDATE profileimg SET status=0 WHERE userid='$id';";
        $result = mysqli_query($conn, $sql);
  
        header("Location: index.php?uploadsuccess");
      }
      else {
        echo "Your file is too big!";
      }
    }
    else {
      echo "There was an error uploading your file, try again!";
    }
  }
  else {
    echo "You cannot upload files of this type!";
  }
   }
  </code></pre>