image can be uploaded but I cant an upload an image for specific user

<?php
            if(isset($_SESSION['id']) ) {
            ($_SESSION['id']) {
                //echo "you're login";
            }
                echo "<form action='upload.php' enctype='multipart/form-data' method='post'>
                    <br>Qr Code:
                    <p><input type='file' name='file' >
                    <p><input type='submit' value='Upload' name='submit'>
                      </form>";
                            }
                ?>
 <?php
                
                $statusMsg = '';
                
                // File upload path
                $targetDir = "qr_code/";
                $fileName = basename($_FILES["file"]["name"]);
                $targetFilePath = $targetDir . $fileName;
                $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
                
                if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
                    // Allow certain file formats
                    $allowTypes = array('jpg','png','jpeg','gif','JPG','PNG','GIF','JPEG');
                    if(in_array($fileType, $allowTypes)){
                        // Upload file to server
                        if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
                            // Insert image file name into database
                            $insert = $db->query("INSERT into qr (file_name, uploaded_on) 
                                                    VALUES ('".$fileName."', NOW())");
                            if($insert){
                                $statusMsg = "The file ".$fileName. " has been uploaded successfully.";
                                    header ("Location:employee.php?uploadsuccess");
                            }else{
                                $statusMsg = "File upload failed, please try again.";
                
                            }
                        }else{
                            $statusMsg = "Sorry, there was an error uploading your file.";
                        }
                    }else{
                        $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF files are allowed to upload.';
                    }
                }else{
                    $statusMsg = 'Please select a file to upload.';
                }
                
                // Display status message
                echo $statusMsg;
                ?>

<?php
                        $query = $db->query("SELECT * FROM qr ORDER BY uploaded_on DESC limit 1 ");
                        if($query->num_rows > 0){
                            while($row = $query->fetch_assoc()){
                                $imageURL = 'qr_code/'.$row["file_name"];
                        ?>
                            <img src="<?php echo $imageURL; ?>" alt="" />
                        <?php }
                        }else{ ?>
                              <img src="images/qr.png" alt="" />
                        <?php } ?>

Welcome to the forums, @qwerty.

Can you explain your issue in more detail?

  1. Uploading image works but it will not show to my specific user in database.
  2. I have two IDs user and admin.
  3. I want to my admin is to be able to display the uploaded images for my user.
  4. The admin can upload the image of the user however the user is unable to view the image uploaded by the admin.

Have you looked at the database table to make sure it looks the way you expect it to?
When you say “the user is unable to view the image uploaded by the admin.” - elaborate for us. What does the user see instead?

When the admin upload an image for user nothing will display but the uploaded image by the admin it will save in database .

“Nothing will display”

I find that to be impossible, given the code I am looking at.

Either the user sees an img tag with something in it, or the user sees an img tag with “images/qr.png” in it. Examine the source on your user-viewed page; what is actually there?

yeah the uploaded image will not display just the default image will show which is the “images/qr.png”.

okay. So your SELECT query is returning 0 results, according to your logic.

is $db a PDO object, or a mysqli object?

It’s mysqli object

I suspect this is a problem of an unstored result returning 0 for num_rows… try this.

 $query = $db->query("SELECT * FROM qr ORDER BY uploaded_on DESC limit 1 ");
                        if($query->num_rows > 0){

becomes:

 $query = $db->query("SELECT * FROM qr ORDER BY uploaded_on DESC limit 1 ");
$resarray = $query->fetch_all();
echo "DEBUG: ResArray Length: ".count($resarray);
                        if(count($resarray) > 0){

error shown:
DEBUG: ResArray Length: 1

S’not an error, its a debug message. Right, so the query pulled back 1 row. That’s a good start.

                            while($row = $query->fetch_assoc()){

=>

                            foreach($resarray AS $row) {

and the code should work.

DEBUG: ResArray Length: 1
Notice : Undefined index: file_name in C:\xampp\htdocs\ims\property1.php on line 54 = $imageURL = ‘qr_code/’.$row[“file_name”];

oh, sorry. fetch_all has defaulted to a numeric array. My bad.

$resarray = $query->fetch_all();

=>

$resarray = $query->fetch_all(MYSQLI_ASSOC);

not working tho.

Can you give me a bit more than ‘not working’? In what way not working? Is an IMG tag appearing in your source now? (It should do; your error message from before indicates that the code is reaching into the foreach…)

let’s say this system have an admin and user account. the admin will upload and display an image for the specific user.

they is no error showing it’s just no displaying image for my specific user when the admin upload image

Where in your code do you specify that the image uploaded is for a specific user?

The code you have written is “Show all images to everyone”. It’s not… not-working, Its that you havent coded something that matches your specifications.

that’s what I’m lacking in my code to be honest I don’t know how get the ID of my specific user to use of my admin

Well, what table are you storing the user’s ID in? How do you know if the logged in user is an admin or not?