简体   繁体   中英

PHP/MySQL Structuring a Table/Database for Profile Usage

I am looking for some direction on how the most efficient way to structure my database for users on my website.

Essentially, I already have a "tblusers" database that includes basic biographical information, such as: tblusers: UsrID, First, Last, DOB, Phone Number, etc.

However, one aspect of my website includes (will include) an area where users can upload multiple pictures of themselves, out of those pictures they can select one to be their default 'profile' picture. Moreover, they can also "crop" any one of their photos to create a "thumbnail avatar" as well.

So basically, I am looking for some help on structuring a table(s) for use of: 1)Multiple pictures, 2)Ability to change default profile picture, 3)Ability to create and use thumbnails/avatars. I'm thinking this will be somewhat based on that user's "usrID", but I'm not sure of the best way to format the table.

Lastly, any suggestions on how to maintain the "file structure" of all the pictures that will be uploaded to different profiles would also be helpful.

Any help would be great! Thank you.

I would create a table for users' images with flags for avatars and profile... (Or create multiple tables for images and avatars)

CREATE TABLE users_images (
    `id` int(10) unsigned auto_increment,
    `userid` int(10) unsigned NOT NULL,
    `name` varchar(255) NOT NULL, -- Filename for the image
    `isavatar` tinyint(1) DEFAULT 0,
    `isprofile` tinyint(1) DEFAULT 0,
    PRIMARY KEY(id)
) ENGINE=InnoDB CHARSET=utf8;

It depends on how much data you want to store in there. The above allows for any number of images to be owned by one user. Personally, I'd likely create a folder structure that uses a users id as its base dir;

 - Uploads
    -user1
     -file1.jpg
    -user2
     -file1.jpg
     -file2.jpg
    -etc

Then you wont need to specify a path in the db, you can create it dynamically using the users' id (i'd keep the user directories as integers too, not using 'user' that was just to demonstrate).

Hope that helps, let me know if you need any more help.

Have you considered using Gravatar (like stackoverflow does) to store user images? It seems to be a cool idea! If you are interested visit www.gravatar.com. Gravatar stands for globally recognized avatars. I would suggest storing user information and images in separate tables. The choice is obvious as there is a one to many relationship between user and images. You can, however, use a variation to improve performance and avoid joins everytime you need to display user data.

User(userID, loginName, loginPwd)
UserImages(imageID, userID, image,type)

The image attribute of UserImages will be a binary object storing the full image or the url to a location on the file system. In the second case, you need code to fetch the image (if required). The type attribute takes on values 'primary profile', 'primary avatar','other profiles'

If the number of images is expected to be very large, you could modify the design as follows

User(userID, loginName, loginPwd, profileImageID, profileImage)
UserImages(imageID, userID, image,type)

SQL to populate the profileImageID and ProfileImage

Update User T1
set (profileImageID, profileImage) = (Select imageID, image 
                                      from UserImages T2
                                      where T1.userID = T2.userID 
                                        and T2.type='primary profile'
                                     )  

It may look like redundant data storage in the users table. This idea is sometimes used to improve performance by avoiding joins. Also, you need to make sure no direct inserts can happen on the Users table for profileImageID and profileImage column by making profileImageID a FK to userImages table. This would still lead to profileImage open to malicious updates. You need special code to handle that. You need to do all this if you expect to be the next Internet sensation! All the best and lets hope you do become one!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM