简体   繁体   English

PHP / MySQL构建用于配置文件使用的表/数据库

[英]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. 本质上,我已经有一个“ tblusers”数据库,其中包含基本的传记信息,例如:tblusers:UsrID,First,Last,DOB,电话号码等。

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. 因此,基本上,我正在寻求一些有关构建表的帮助,以供使用:1)多张图片,2)可以更改默认个人资料图片,3)可以创建和使用缩略图/头像。 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. 我认为这将基于该用户的“ usrID”,但是我不确定格式化表格的最佳方法。

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; 就个人而言,我可能会创建一个使用用户ID作为其基本目录的文件夹结构。

 - 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). 然后,您将不需要在db中指定路径,可以使用用户的id动态创建它(我也将用户目录也保持为整数,而不是仅仅为了演示而使用“ user”)。

Hope that helps, let me know if you need any more help. 希望能有所帮助,如果您需要其他帮助,请告诉我。

Have you considered using Gravatar (like stackoverflow does) to store user images? 您是否考虑过使用Gravatar(像stackoverflow一样)来存储用户图像? It seems to be a cool idea! 这似乎是一个好主意! If you are interested visit www.gravatar.com. 如果您有兴趣,请访问www.gravatar.com。 Gravatar stands for globally recognized avatars. Gravatar代表全球认可的化身。 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. UserImagesimage属性将是一个二进制对象,用于存储完整图像或指向文件系统上某个位置的url。 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' type属性采用值“主配置文件”,“主头像”,“其他配置文件”

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 SQL填充profileImageID和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. 此外,您需要通过将profileImageID设置为userImages表的FK来确保在users表上不会对profileImageID和profileImage列进行直接插入。 This would still lead to profileImage open to malicious updates. 这仍然会导致profileImage对恶意更新开放。 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! 祝您一切顺利,并希望您成为一体!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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