Skip to main content
Participating Frequently
September 13, 2009
Answered

How do I upload multiple images to d-base through a single page?

  • September 13, 2009
  • 2 replies
  • 1367 views

I can upload a single image with a form to my MySql database with no problem.  It sends the file name to the database then stores the image in an image file.

But now I want to upload multiple images in the same way from the same PHP page.  Each multiple upload needs to be stored to a single record (designated user that's logged in).  I have a field for each image in the database.

This may give you a better idea of what the database looks like:

UsernameIDimage1image2image3
  scmeeker5dotty.jpghello.jpgkitty.jpg
jdmeeker7

So now I want to upload multiple images for each image field.  When it works correctly, it should send the name of the image file to the proper field and then send the image to the image folder.  I've tried it several ways with no luck at all.

Any one have any ideas???

Thanks!

This topic has been closed for replies.
Correct answer UteFanJason

I think you would be better off having two tables in you database. One would be a users table that has all of the user data including a user id (the primary key for this table and the foreign key for the other table). The second would be an images table. You would use the images table to upload infinite (well, as many as the db can handle) images. The trick, is that each record in the images table would have a foreign key, which would be the user id for the user that uploaded the image.

This way your users wouldn't be limited to three images but could upload as many as you want to allow them to. The association of the user id, which has to be unique to each record in the users table (there cannot be duplicates, it should be your primary key on the users table), to the user id in any number of records in the images table is what will allow this to work.

EX:

The users table

user_idf_namel_nameemailother_info
100001firstlastemail@email.com...
100002f_namel_nameemail@example.com...

The images table

image_idsrcuser_id
10000000001/db_imgs/img1.jpg100001
10000000002/db_imgs/img2.jpg100001
10000000003/db_imgs/img3.jpg100002
10000000004/db_imgs/img4.jpg100001
10000000005/db_imgs/img5.jpg100001

2 replies

David_Powers
Inspiring
September 14, 2009

This thread has been moved to the Dreamweaver Application Development forum, which deals with server-side issues.

UteFanJason
UteFanJasonCorrect answer
Inspiring
September 14, 2009

I think you would be better off having two tables in you database. One would be a users table that has all of the user data including a user id (the primary key for this table and the foreign key for the other table). The second would be an images table. You would use the images table to upload infinite (well, as many as the db can handle) images. The trick, is that each record in the images table would have a foreign key, which would be the user id for the user that uploaded the image.

This way your users wouldn't be limited to three images but could upload as many as you want to allow them to. The association of the user id, which has to be unique to each record in the users table (there cannot be duplicates, it should be your primary key on the users table), to the user id in any number of records in the images table is what will allow this to work.

EX:

The users table

user_idf_namel_nameemailother_info
100001firstlastemail@email.com...
100002f_namel_nameemail@example.com...

The images table

image_idsrcuser_id
10000000001/db_imgs/img1.jpg100001
10000000002/db_imgs/img2.jpg100001
10000000003/db_imgs/img3.jpg100002
10000000004/db_imgs/img4.jpg100001
10000000005/db_imgs/img5.jpg100001
scmeekerAuthor
Participating Frequently
September 14, 2009

I am going to try this.  Thank you for your reponse. 

One question though.  Since I can't have a primary key in each table, can the imageID that you created in the second table have auto numbering?  It seems I couldn't apply that when I tried.

I'm fairly new to MySql, so I hope you can bear with me.  Thank you. 

UteFanJason
Inspiring
September 14, 2009

scmeeker wrote:

I am going to try this.  Thank you for your reponse. 

One question though.  Since I can't have a primary key in each table, can the imageID that you created in the second table have auto numbering?  It seems I couldn't apply that when I tried.

Each table should have its own primary key field. In the users table you would want to call it user_id or something similar. In the images table you would have the image_id (or something named similar) be set up as the primary key for that table.

When I said you can't have duplicates I only meant that you can't have two instances of the user_id in the users table. For example, you could not have multiple records with their id as 100001. You can set MySQL to AUTO_INCREMENT a given field, such as a primary key.

However, you can have multiple instances of the user_id in the images table. That is called a foreign key. It is the primary key for a different table. It is a way to associate multiple sets of data (or images) to one record (user) in another table.

If you are using MySQL along with PHP you should look into using PHPMyAdmin. It provides a GUI for all of this that is easy to use and very reliable.

I'm fairly new to MySql, so I hope you can bear with me.  Thank you. 

No worries. I am new to plenty of things myself, like flash. I understand the feeling of not knowing how things work.