I have a social network site which allows users to upload an image as a profile cover. I use PHP to upload the image to database and select it to display as profile cover.The image shows perfectly, but when I visit a different page and come back, the cover image does not show up even though I'm still logged in. I must re-select another image.

something must be wrong

<?php
if(isset($_SESSION['id'])){
if(isset($_POST['submit_cover'])){
    $userId = $_SESSION['id'];
    $file = $_FILES['cover_image'];
    $filename = basename($file['name']);
    $fileerror = $file['error'];
    $filetmp = $file['tmp_name'];
    $filePath = "coverimages/".$filename;
    $filetype = pathinfo($filePath,PATHINFO_EXTENSION);
    $allowedExt = array('jpg','jpeg','png','gif');
    if(in_array($filetype,$allowedExt)){
        if(!file_exists($filename)){
            if($fileerror == FALSE){
                move_uploaded_file($filetmp,$filePath);
                $sql = "UPDATE users SET usersCover='$filePath' WHERE usersID='$userId'";
                mysqli_query($conn,$sql);
                $sql = "SELECT * FROM users WHERE usersID='$userId'";
                $query = mysqli_query($conn,$sql);
                if($row = mysqli_fetch_array($query)){
                    $coverImage = $row['usersCover'];
                }
            }else{
                echo "<h4 style='color:red;'>Image is invalid</h4>";
            }
        }else{
        echo "<h4 style='color:red;'>Image already taken!</h4>";    
        }
    }else{
        echo "<h4 style='color:red;'>Image not allowed!</h4>";
    }
 }
}

Oh, guys. I have kinda figured it out. I separated the UPDATE and the SELECT and everything looks great

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.