简体   繁体   English

简单照片共享站点:如何为多个用户及其照片构建MySQL数据库

[英]Simple Photo Sharing Site: How to structure the MySQL database for multiple users and their photos

I am a high school student learning PHP and MySQL. 我是一名学习PHP和MySQL的高中生。 I am trying to create a simple photo sharing site that allows a user to sign up and start uploading images. 我正在尝试创建一个简单的照片共享网站,允许用户注册并开始上传图像。 Each user has a profile that shows all of their pictures. 每个用户都有一个显示所有图片的个人资料。

I currently only have one table, called "users": username, email, password 我目前只有一个名为“用户”的表:用户名,电子邮件,密码

How should I set up a table for the pictures? 我该如何为照片设置一张桌子? And how do I link to each picture's filepath? 我如何链接到每张图片的文件路径?

Thanks 谢谢

On your user table create a column called Id (this should never change) You normally use something like an INT AUTOINCREMENT number for this so the database allocates it automatically, and this should be marked as the PRIMARY KEY 在您的用户表上创建一个名为Id的列(这应该永远不会更改)您通常使用类似INT AUTOINCREMENT数字的内容,因此数据库会自动分配它,这应该标记为PRIMARY KEY

Create a table called photos 创建一个名为照片的表格

Table photos
Id = autoincrement primary key
UserID = int (you can set this up as a foreign key, but to keep it simple, just make it indexed)
Title = varchar
Info = varchar
Filename = varchar

Table Photos
Id      |  UserId  | Title    | info | Filename  | (add other columns to suit)
------------------------------------------------------------
1       |  1       | Duck     | xyz  | duck.jpg  | 
2       |  1       | Cat      | xyz  | cat.jpg   | 
3       |  2       | Mouse    | xyz  | mouse.jpg | 

As you can see from the data above Photo 1 and 2 belong to user 1 and photo 3 belongs to user 2 从上面的数据可以看出,照片1和2属于用户1,而照片3属于用户2

To select all the photos for username 'bob' you would do something like this 要为用户名'bob'选择所有照片,您可以执行以下操作

SELECT Photos.Id, Photos.Filename 
FROM Photos 
JOIN Users ON Photos.UserId=Users.Id
WHERE Users.username = 'bob'

The first question is where are you storing the user's photos? 第一个问题是你在哪里存储用户的照片? I would recommend this: 我会推荐这个:

Table (User's):
Username | Password | Email
---------------------------
user1    | fdgfdg   | fdfgdf
user2    | ffdgfd   | fgdfgf
user3    | dgfdgg   | fgdgdd

Then, whenever a user signs up, create a folder with that user's name and a subdirectory titled photos. 然后,每当用户注册时,创建一个包含该用户名称的文件夹和一个名为照片的子目录。

Create a Table for Each User. 为每个用户创建一个表。

Table user1
Photo     | Title    | info
----------------------------
duck.jpg  | My Duck! | fgdf
cat.png   | Le Cat   | dfgd

Then, when a user logs in, list all the Titles and link them to the photo dynamically. 然后,当用户登录时,列出所有标题并动态链接到照片。

So I would be 所以我会

My Duck --> http://mysite.com/user1/photos/myduck.jpg

Where user1 would be the logged in username, the photos would automatically be added and myduck.jpg would be pulled from the database. 如果user1是登录用户名,则会自动添加照片,并从数据库中提取myduck.jpg。

However, this opens up two holes: - Hotlinking - If someone knows the username and photo title, they can access the photo without being given permission. 但是,这会打开两个漏洞: - 热链接 - 如果有人知道用户名和照片标题,他们可以在未经许可的情况下访问照片。

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

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