Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Integrating PHP with Databases Querying the Database with PHP Integrating Database Results

running on LAMP, pictures wont display

Notice: Undefined index: img in /var/www/html/php-integrating-db/inc/functions.php on line 6

function get_item_html($id, $item){ $output = "<li><a href='details.php?id=" . $id . "'><img src='" . $item["img"] . "' alt='" . $item["title"] . "'/>" . "<p>View Details</p>" . "</a></li>"; return $output;

}

what is going on here now? . $item["img"] . "' alt='"

8 Answers

Alex Misnovs
Alex Misnovs
12,163 Points

If this is still valid to someone.. I ran into the same issue as probably Brendan had.

In my case it was easy. Original code in connection.php wasn't actually selecting img from the database. So there was nothing to display.

$results = $db->query("SELECT title, category FROM Media");

Make sure that you add img to the SQL query :)

$results = $db->query("SELECT title, category, img FROM Media");

And I suppose it will work..

Thanks, it worked for me!

If you are working locally, the images aren't displaying because they aren't in your LOCAL /img/media folder. Get the project files from the downloads section, you'll find the missing images in the integrating_php_with_databases_s1v1/img/media folder. Drag them all into your LOCAL /img/media folder with the other images. Refresh the Personal Media Library page and you should be good to go.

[ Note: If the image for "Star Wars: The Ultimate Soundtrack Collection" still doesn't display, that's due to a separate issue. If you look for it in the media table of your database you'll notice that it is missing the .jpg file extension. View your database with phpMyAdmin and click on the 'search' tab. Then click 'find and replace.' Find 'soundtrack_star_wars' in the 'img' column and replace it with 'soundtrack_star_wars.jpg'. Now go check that it shows up under Full Catalog>Music of the Media Library page. ]

Simon Coates
Simon Coates
28,694 Points

To make the problem clear, you get the same notice with this snippet.

$array = [];
echo $array['doesnotexist'];

You need to check whether $item actually has the value you want and whether you are simply using the wrong index ("img"). If the value is not provided, then you will need to take steps to provide. (I'm not sure but if you're following along with a video series, this may be expected. occasionally, they gloss over issues until later).

Thats the thing, it works fine in the video.

Simon Coates
Simon Coates
28,694 Points

so you output the variable using something like var_dump to see if the array contains an 'img' index. If it does then good, if not you look at where the $item parameter is coming from. The most obvious differences would be if you're missing a field or alias in the SQL.

it does say its missing 'img' however I can see them in the downloads folder, they are not apart of the DB.

Simon Coates
Simon Coates
28,694 Points

"img" is not the image, but a path that associates each item to its related image. That was stored in the associative array in data.php and is stored in the database.

Not sure how I fixed it but its fixed.

Joshua Lawson
Joshua Lawson
4,953 Points

I would like to know what you did, because I have the exact same issue in xampp.

Make sure your selection in the query statement matches the path in the function.

The issue for me was that I was using the download img/media folder from the PHP Beginners course. The img/media folder for this Intermediate course has a lot more images. I also found that two of the images had incorrect paths, these images were "Lord of the Rings" and "Star Wars Soundtrack".