Link to home
Start Free TrialLog in
Avatar of ReekaJean
ReekaJean

asked on

Uploading Images to a Category/SubCategory with PHP/MySQL

Okay - I'm attempting to create an 'admin' section of my website so I can upload my images into a category or appropriate subcategory instead of having to manually add them/code them on the HTML pages....

I have three tables:

icon_categories, icon_subcats, icon_images.

the categories table has 'cat_id' and 'cat_name', icon_subcats has 'sub_id', 'sub_name', 'cat_id', and the icon_images table has 'icon_id', 'icon_filename', 'icon_caption', 'cat_id', 'sub_id'.

I want to have two drop down boxes, the file field to upload the image, and then a text field to input the caption.

The first drop down box will be categories, and will be populated from the mysql database. The second drop down will be subcats - and I want it to populate with the subcats that match the original category selected. (IE When I select the 'movies' category, I want the second drop down to populate with all the subcats that go in movies).

That is what I'm having the first problem with. I've tried modifying several scripts... but finally just gave up. o.O

Then, I need the file to upload, I'm not picky about renaming or anything like that... I just need it to upload, and be stored in the database so that when I get around to coding the 'view' part of the album, I'll be able to pull it from there.

I don't need thumbnails, as all the images are only 100x100 pixels anyway.

Any help would be greatly appreciated... I'm pulling out my hair trying to figure out how this would work.
Avatar of ldbkutty
ldbkutty
Flag of India image

Regarding dynamic dropdown box, you can use with Javascript or PHP. Javascript is client side solution and does not need to be reloaded, but it might have been disabled by the user. PHP is serverside solution and needs to be reloaded when a selection is made.

Storing the images in files is less complex and faster than database blobs. Upload the images to a common folder in the system and store the path in the database.

Tell me which of these you prefer.
Avatar of ReekaJean
ReekaJean

ASKER

I'm really not picky on the javascript/php option. This is only going to be used by me as an easier means of uploading my graphics/zip files and keeping them organised. So as far as that goes - just whichever way you think would be better/easier.

And I did want the files to be stored on the server. The image directory is /icons/ for these in particular... I just want the filename to be stored in the mysql so it will be easier for me to pull them back out in table form later on. :)

I wasn't going to worry about having seperate folders for each cateogry/subcat.... so something will need to be done to make sure the images aren't being named the same thing... either by keeping my original filename or just making sure that the php doesn't name them something that's already there.

But if I can get this working - I won't be adding any of the images myself... I'll be doing it all through the upload/gallery thing that I'm trying to build....

so like, I'd have icons/ and then *all* the graphics that I've uploaded will be stored in that folder... the database will call the filename and my caption as well as whichever category/subcategory ID that i've placed them in.
can you post the table structure + some related data's (as .sql statements) so I can give you direct solution.
Alright.... this is in php form, so if you save it and run it - it should populate everything. This creates the three tables that I've currently got set, and adds the categories and subcats that I have so far. I don't have any images uploaded yet - so the image table is still blank. :)

----------------------------
mysql_query("CREATE TABLE icon_categories(
                                    cat_id INT NOT NULL AUTO_INCREMENT,
                                    cat_name VARCHAR(255),
                                    PRIMARY KEY (cat_id)
                        )") or die ("Table Creation Error." .mysql_error());

mysql_query("CREATE TABLE icon_subcats(
                                    sub_id INT NOT NULL AUTO_INCREMENT,
                                    sub_name VARCHAR(255),
                                    cat_id INT(20) NOT NULL,
                                    PRIMARY KEY (sub_id),
                                    KEY (cat_id)
                        )") or die("Table Creation Error." .mysql_error());
                        
mysql_query("CREATE TABLE icon_images(
                                    icon_id INT NOT NULL AUTO_INCREMENT,
                                    icon_filename VARCHAR(255),
                                    icon_caption TEXT,
                                    cat_id INT(20) NOT NULL,
                                    sub_id INT(20),
                                    PRIMARY KEY (icon_id),
                                    KEY (cat_id),
                                    KEY (sub_id)
                        )") or die ("Table Creation Error." .mysql_error());

mysql_query("INSERT INTO icon_categories (cat_name) VALUES ('Movies'), ('Music'), ('Actors'), ('Actresses'), ('Misc')") or die("Could not populate database." .mysql_error());

mysql_query("INSERT INTO icon_subcats(sub_name, cat_id) VALUES ('Harry Potter', 1), ('Lord of the Rings', 1), ('Phantom of the Opera', 1), ('Star Wars', 1), ('Johnny Depp', 3), ('Orlando Bloom', 3), ('Alan Rickman', 3), ('Drew Barrymore', 4), ('Angelina Jolie', 4), ('Humour', 5), ('Animals', 5), ('Holidays', 5)") or die("Could not populate database." .mysql_error());

ASKER CERTIFIED SOLUTION
Avatar of ldbkutty
ldbkutty
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Only one problem.... I've got this in the admin section of my site...

The path to the icons directory is up one level, and the ../icons/ isn't working. o.O

Is there something special I need to do so it reads the path instead of thinking the two dots are supposed to be there? lol.

And thank you so much for this by the way. ^^
Is the "icons" a directory placed one level up for the current php file ? If yes, that should work.

Just for information,

This path will take you to a file within the same directory :
<img src="picture_name.jpg" />

This path will take you one directory back :
<img src="../directory_name/picture_name.jpg" />

This path will take you two directories back :
<img src="../../directory_name/picture_name.jpg" />

Or you can give full URL for testing.
Ah. Thank you. It was a typo on my part. lol.

Sometimes it just takes someone else to say something before you see it. hehehe.

Thank you so much! :)
rtrtrfgf6trrtewews