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

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?

same table but with different IDs .I make two PHP page for user and admin so I can login as user or admin

This would be an input in the upload form. You would build a select/option menu to let the admin(istrator) select the user id that the image will be associated with.

In the form processing code, after you have validated all the submitted form data, you would store the submitted user id in the row that’s inserted into the qr table.

When you display the qr image for the currently logged in user, you would get the user id from the session variable and use it in the WHERE term in the SELECT query to find the matching row in the qr table, if any.

1 Like

Mabismad beat me to the Reply button but I will post what I wrote anyway.

So you’ve got a DB table for “users”. This table should have field that is of type int that is set as the PRIMARY KEY and AUTO_INCREMENT. This field might be called id or user_id but the point is this will be the unique user ID that identifies the user. All your pages should have session_start(); at the top before anything is sent to the browser. This will allow you save important information about the user when they log in as you normally are making a query to check their login information, so grab fields about the user you want e.g. id, level (which could hold ‘Admin’ or ‘User’) and set these values to session with the query result. As an example:

$_SESSION['secure_id'] = $row['id'];	
$_SESSION['secure_level'] = $row['level'];

Now any displays or actions can be based on this information. For example saving uploaded files can now be saved with the user ID and as mentioned you can query for images that belong to this user as their $_SESSION['secure_id'] holds that information and identifies the user viewing the page.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.