简体   繁体   中英

Mysql Insert Data into multiple tables from array

在此处输入图片说明

 Tags array(11) {
  ["album"] => string(17) "Away From The Sun"
  ["artist"] => string(12) "3 Doors Down"
  ["bitrate"] => int(256000)
  ["title"] => string(16) "Ticket to Heaven"
  ["filename"] => string(23) "04 Ticket To Heaven.mp3"
  ["format"] => string(3) "mp3"
  ["play_time"] => float(207.5950625)
  ["genre"] => string(11) "Alternative"
  ["year"] => string(4) "2002"
  ["track"] => string(1) "4"
  ["art"] => string(21) "Away From The Sun.jpg"
}

I need help with taking the data from this array and populating these tables with the data and maintaining the relationships.
I extract data from these kinds of tables all the time and I build them with known data.
In this case the data is unknowable to me until I parse the id3 tag.
All 3 tables are auto incrementing on 'id'.
I typically work in Zend Framework, so answers in ZF are really helpful but php and sql are almost as good.
I found one question SQL Insert with data from multiple tables that seems to address this issue, but I don't have the sql to understand the answer.
Would a link table help?

PS I know the relationship lines in the figure are incorrect.

Since the relationships depend on autoincrement IDs probably the best thing to do is to insert into the artist table and then call $artist_id = mysql_insert_id() to get the new ID number. You can then insert into album and call the same function as $album_id = mysql_insert_id()`. Finally insert your tracks using the IDs you just retrieved.

You'll be able to ensure you do not have albums without artists or tracks without albums, and you'll need to insert albums and tracks without creating new artists or albums, so this should easily suffice.

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