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

General Discussion

php problem

hi everybody

***** i have a homework that need to submit the little cart. i feel frustrated in the problem of

images upload. for example , i am making a pet shop, and i have mysql tables like ....e.g.

category , products , photos, since now i just try to have little categories for experiment, like cat

or birdie, and users upload the photos to the site, and i have a backend admin to monitor the

products, i want to get photos related to "specific product" or related to "specific product of

certain category" from those table photos , can someone let me know how i can write the sql

statement for this case. (for example i want to get the dog category photos only to the category

page)

**** Also for multiple file upload , it seems that there is a thing like below

uploads[ ] is array right? and how can i make use of this and so that when upload to mysql

photos table, those records are related to those products.

Thank you

1 Answer

Not quite following you here.. but here are a couple examples: -you want to select all photos with a category of dogs (Assuming that your table includes at least the photo name and category) SQL: SELECT photos FROM photo_table WHERE category="dogs"

-you want to select all photos where the product is "shampoo" and category is "dogs". (Assuming that here you have a table with the photo name, product type, and category. SQL: SELECT photos FROM photo_table WHERE category="dogs" AND product="shampoo"


For the file upload...

assuming you are using a form something like this:

<form action="file-upload.php" method="post" enctype="multipart/form-data">
<Send these files:<br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input name="cat" type="text"/>
<input type="submit" value="Send files" />
</form>

then you would access the uploaded files like so:

$_FILES['userfile']['name'][0]
$_FILES['userfile']['name'][1]

see: http://www.php.net/manual/en/features.file-upload.multiple.php

So then to insert into your table you'd cycle through the array:

$_POST['cat'] = $category
foreach ($_FILES['userfile']['name'] as $index => $filename) {
mysql_query ("INSERT INTO 'photo_table' (photo_name, category) VALUES ('$filename','$category')");