Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Good evening all,

I have a question regarding allowing each user id upload a profile picture. I'm building a dynamic website that allows users to register and have their own profile. In their profile, they can upload a profile picture. I have many tables in my database, including a table for registration which has the user_id as the primary key, and a table for uploading profile pictures which has picture_id as the primary key and user_id as the foreign key.

Now here's the problem: When I login, the user_id is inserted into the database, but when I go to the profile page to upload a picture, I get a "user id does not exist" error (I added that error message). Then I'd have to go to the profile picture table and manually add the user_id to the table and then the profile picture gets uploaded.

So, my question is, when a user logs in and the user_id(primary key) in the registration table is inserted, is there a way to also update and insert that same id in the user_id(foreign key) in the profile picture table without doing it manually? So that each user can have his own profile picture.

PS: I'm not uploading the pictures themselves into the database, i'm inserting the path of the image into the database.

I appreciate any help.
Thanks!
Posted

1 solution

First of all, why would you need to insert a record into user data table when the user LOGS IN? You should insert that record only when the user SIGNS UP. And reuse the same record every subsequent time the user logs in.

You can insert a record to the profile picture table the same way you inserted data to the user table. Write a mysql_query to insert that data into profile picture table and place it after the mysql_query to insert a record to users table.

But wait. Assuming you have one-to-one mapping with user and profile picture, why would you ever want to store the profile picture in a separate table. Surely you have the same keys in two tables. Move all the columns in the profile pictures to the users table. And before doing that, Google "database normalization" and read some of the links that pop up, because this is BAD NORMALIZATION. (Of course this is assuming you have one-to-one mapping with user and profile picture. If not, ignore.)
 
Share this answer
 
Comments
AmeeR-89 18-Feb-12 4:35am    
Thank you sooo much for your help and for answering my question.

Yes, you are right, the record needs to be inserted in the sign up table, not login. My bad sorry.

And yes, it's a one-to-one mapping with the user and profile picture.

Now I have the records in two separate tables and just to make sure that I understood what you said correctly, I need to add the profile records and fields into the users table, is that right?

So in the users table, I have the sign up information of the user, including user id(PK). In the profile table, I have user id(FK) and location of image as columns in that table.

So I need to have all records of the profile table stored in the users table? And drop the user id(FK) column, right?


Thanks again for your help! :)
krumia 19-Feb-12 23:31pm    
My idea was, bring all the columns in profile pictures table to users table, and get rid of (delete) the profile picture table.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900