doudiza9154 2012-02-14 08:02
浏览 56
已采纳

Mysql将数据从数组插入多个表

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.

  • 写回答

1条回答 默认 最新

  • duanchoupo1104 2012-02-14 08:12
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动