Solved

How to set up mysql database to hold multiple image path fields

Posted on 2007-11-26
5
690 Views
Last Modified: 2011-09-20
I've currently got a script which uploads one image to my server and stores the path to that image in my database.
However I want the user to be able to upload more than 1 image at a time (probably around 12). Now in php I can easily upload multiple images using a loop, that's not the problem.
My problem is how do I store the paths to all uploaded images in my database?
Would I have to add more path fields? (like path1, path2, path3 etc.)

Currently my image table cosists of ID, Name, Path fields.

Can anyone shed some light on this please? it's been bugging me for too long.
0
Comment
Question by:thephiller
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:bdichiara
ID: 20353932
I would suggest associating these images in groups or albums (like with GroupID or AlbumID). Then you can have multiple records in your table all grouped together. Then give the group some meaningful title or link that to an album or whatever.
0
 

Author Comment

by:thephiller
ID: 20354012
The images is associated with an album table so I'm using one table to store the album details and one table to store the images and image details.

I'm more after how I can technically achieve this since I'm kind of lost at the moment.
0
 
LVL 7

Expert Comment

by:bdichiara
ID: 20354280
Well, I don't know what your code looks like, but when you loop through the uploads, insert each one as a record into the image table. and just set the album id to be the same for the entire upload.
0
 

Author Comment

by:thephiller
ID: 20356831
How would I go about setting the id for the entire upload?

Now when it goes through the upload script it adds to the image table:
ID
NAME
PATH

My problem (sorry, if I'm having a bit of a hard time understanding this) is how I store the individual images. Would I have to add more path fields?
Like:

ID
NAME1
PATH1
NAME2
PATH2

Or do you suggest I DON'T make the id field in the image table auto increment but assign the id myself?
Sorry, I'm still a little lost.
0
 
LVL 7

Accepted Solution

by:
bdichiara earned 500 total points
ID: 20362700
The ID in the table mentioned above is the ID for the individual image, correct? You should have:
ID - int - auto_increment
NAME - varchar
PATH - varchar
ALBUM - int

Then in a separate table
ALBUM_ID - int - auto_increment
DESCRIPTION - varchar

Then you either create a new album (using auto-increment, and get the ID with mysql_insert_id) or you can have the user select an existing album.

Use this ALBUM ID and put in all instances of ALBUM for that upload.

So in plain terms:

If(new_album?){
    INSERT 1 row into ALBUMS
    $ALBUM_ID = mysql_insert_id(); (php function)
} else {
    $album_id = USER selected album.
}

upload_loop {
   DO File placement stuff...
   INSERT file path, name, album_id into IMAGES table
   (you get the album_id from above)
}

Does that make better sense?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now