简体   繁体   中英

Insert values with auto-increment foreign keys

I have a database 3 tables: matrix, mark and matrix-mark (the relationship between matrix and mark is NN).

Here are the attributes of each one:

matrix: mx_id (auto-incremented); name;
mark: mk_id (auto-inc); name; color;
matrix-mark: mk_id (FK); ma_id(FK). 

For example, how would i insert multiple marks associated with the same matrix? I'm getting the values through php post.

Thanks in advance.

In php on inserting the record u can get the last inserted id. there is a function for this.

mysql_query("INSERT INTO matrix(name) values ('deepak')");
$matrix_id = mysql_insert_id();

mysql_query("INSERT INTO marks(name,color) values ('deepak','white')");
$marks_id = mysql_insert_id();
// now u can insert these two id in your third table.. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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