简体   繁体   English

MYSQL 存储多张图片

[英]MYSQL storing multiple images

I have a web page where users can upload articles with images on them.我有一个网页,用户可以在其中上传带有图片的文章。 There is not limit on the amount of images a user can upload with their article.用户可以随文章上传的图片数量没有限制。 Each row in a MySQL database represents one article of many. MySQL 数据库中的每一行代表许多文章中的一篇。 What is the best way to store all these images.存储所有这些图像的最佳方法是什么。 I know I would use BLOBS/LONGBLOBS but if I have no control over the amount of images a user uploads I can't just insert 50 columns for different images and hope they upload less than 50. What is the best way to do this.我知道我会使用 BLOBS/LONGBLOBS,但如果我无法控制用户上传的图像数量,我不能只为不同的图像插入 50 列并希望它们上传的数量少于 50。这样做的最佳方法是什么。

The best way to do this is to create a table with images that references the article so it will leave you with a database like this:最好的方法是创建一个包含引用文章的图像的表格,这样它就会为您留下这样的数据库:

数据库图

The table creation script for Images would be like this: Images的表创建脚本如下所示:

CREATE TABLE `Images` (
    `ImageID` bigint NOT NULL AUTO_INCREMENT,
    `ArticleID` bigint NOT NULL,
    `Image` longblob NOT NULL,
    PRIMARY KEY (`ImageID`)
);
ALTER TABLE `Images` ADD CONSTRAINT `FK_Images` FOREIGN KEY (`ArticleID`) REFERENCES `Articles`(`ArticleID`);

Now all you have to do is insert an image into Images together with the ArticleID it belongs to.现在您要做的就是将Images连同它所属的文章ArticleID一起插入到Images中。

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

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