同时插入mysql数据库中的两个表 [英] insert into two tabels in mysql database at the same time
问题描述
我有一个数据库,我应该可以让用户将多张图片上传到他的帐户,
I have a database, which I should able user to upload multiple images to his account,
如何在 MySQL 数据库及其 PHP 代码中执行此操作以插入它们.
How could I do this in MySQL database and its PHP code to insert them.
这是我尝试过的,我创建了 2 个表
Here what I tried, I created 2 tables
CREATE TABLE `user` (
`userID` int(11) unsigned NOT NULL auto_increment,
`imageID` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE `iamges` (
`imageID` int(11) unsigned NOT NULL auto_increment,
PRIMARY KEY (`imageID`)
)
所以,每个用户可以通过imageID上传多张图片,在images表中我会放image1,image2等
so, each user can upload multiple images through imageID and in the images table I will put image1, image2 etc.
当我想选择它们时,我会使用:
When I want to select them I will use :
SELECT user.name, Persons.email, images.image1, images.image2
FROM user
INNER JOIN images
ON user.imageID=images.imageID
ORDER BY Persons.name
但是,当用户上传多张图片时,我怎么能一次插入到 2 个表中?
But, how could I do insert into the 2 tables at once when the user upload multiple images ?
推荐答案
使用所有权表.基本上是带有 id 和 image 列的第二个表,并允许用户每个都有多行.然后您将检索具有用户 ID 的所有行.这是存储一对多"关系时的常用方法,例如 Bob 拥有锅、平底锅、勺子;或者鲍勃是吉姆、汤姆、亚历克斯、金的朋友.
Use an ownership table. Basically a second table with columns id and image and allow users to each have multiple rows. Then you would retrieve all rows with the users id. This is a common approach when storing "one to many" relationships like Bob owns a pot, pan, spoon; or Bob is friends with Jim, Tom, Alex, Kim.
这篇关于同时插入mysql数据库中的两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!