2

enter image description here

 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?

P.S. I know the relationship lines in the figure are incorrect.

Community
  • 1
  • 1
RockyFord
  • 8,529
  • 1
  • 15
  • 21

1 Answers1

1

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.

Ilion
  • 6,772
  • 3
  • 24
  • 47
  • I was hoping for something more elegant then just cascading queries. – RockyFord Feb 14 '12 at 13:55
  • Elegance is not achieved by over-complicating what you are trying to do. An artist will have many albums and an album will have many tracks, so most of the time you won't be adding new rows to these tables. Think about what will make your code easiest to understand. – Ilion Feb 14 '12 at 16:11