简体   繁体   English

如何将值插入联结表?

[英]How to insert values into a junction table?

My question is a little bit long, I am sorry for that. 我的问题有点长,对此我感到抱歉。

I add values into my two tables using the code below, but I also have a junction table called book_cover and I want to add data into this table to make a connection between book_id and metadata_image_id (since a book has more than one cover images), how can I do it when I add the records for the first time? 我使用下面的代码将值添加到两个表中,但是我还有一个名为book_cover的联结表,我想向该表中添加数据以在book_id和metas_image_id之间建立连接(因为一本书具有多个封面图像),第一次添加记录时该怎么做?

I couldn't make the connection even though I read about joins. 即使我了解联接,也无法建立连接。

I am using MySQL and InnoDB. 我正在使用MySQL和InnoDB。

Here is my PHP code to add data: 这是我的PHP代码来添加数据:

$stmt = $sqli->prepare("INSERT INTO book(book_original_name, book_publication_date, book_synopsis) VALUES (?,?,?,?)");
$stmt->bind_param("sss", $_POST['book_original_name'], $_POST['book_year'], $_POST['book_synopsis']);
$stmt->execute();
$stmt->close();

$stmt = $sqli->prepare("INSERT INTO metadata_images(metadata_image_value, type, size) VALUES (?,?,?)");
$stmt->bind_param("sss", $name, $_FILES['book_cover']['type'], $_FILES['book_cover']['size']);
$stmt->execute();
$stmt->close();

For instance, when I added the book that has the id of 50 and metadata_image has the id of 92, I also want to add book_id and metadata_image_id to my junction table to create the relation between a particular book and image. 例如,当我添加ID为50的书而metadata_image具有ID为92的书时,我还想将book_id和metadata_image_id添加到我的联结表中,以创建特定书与图像之间的关系。 What kind of SQL query should I use to achieve that? 我应该使用哪种SQL查询来实现这一目标?

My database structure: 我的数据库结构:

Table : book
book_id (PK and AI)
book_original_name
book_publication_date
book_synopsis

Table : metadata_images
metadata_image_id (PK and AI)
metadata_image_value
type
size

Junction Table : book_cover
book_id (Foreign key from book)
book_metadata_id (Foreign key from metadata_images)

If you know that you just inserted the book with ID 50 and the image with ID 92, and these are directly related (ie, that book has that image on its cover), then the query to populate the junction table is simply: 如果您知道刚插入ID为50的书和ID为92的图像,并且它们是直接相关的(即,该书的封面上有该图像),则填充联结表的查询很简单:

INSERT INTO book_cover VALUES (50, 92);

There is no secret or special query for junction tables. 联结表没有秘密或特殊查询。 The trick is efficiently determining which values to insert; 技巧是有效地确定要插入的值。 for that, use MySQL's LAST_INSERT_ID() function, which you can access via the mysqli API as mysqli::$insert_id . 为此,请使用MySQL的LAST_INSERT_ID()函数,您可以通过mysqli API作为mysqli::$insert_id

You must get the ID immediately following the INSERT statement that generated it, in the same session. 您必须在同一会话中紧随生成该ID的INSERT语句之后获取该ID。 You can save it in a user variable, perform the next insert, and then use the insert ID from the second table along with the ID saved from the first table as the parameters for your insert to the junction table. 您可以将其保存在用户变量中,执行下一个插入,然后将第二个表中的插入ID以及从第一个表中保存的ID用作插入联结表的参数。

It's a good idea to wrap all of this in a transaction so that you don't end up inserting only part of a book's information; 将所有这些都包装在事务中是一个好主意,这样您就不会最终只插入一本书的信息的一部分。 either all three queries (or however many) should succeed, or all should fail. 所有三个查询(或许多查询)都应成功,否则所有查询都将失败。

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

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