dongwa3808 2013-03-30 17:55
浏览 82

更新和插入数组等

I have a MySQL db called table_music Here I store artist, song,... this is something I will update from a form I have on a page.

Now I want to add genres to this form as well, so I made a table called song_genre Here I have song_genre_id, song_id, genre_id

In my form I list the title of song from my table_music Then I have fields where I can update the composer, length, year, and so on. And there is also a checkbox list called genre from my song_genre table with all the genres sorted alphabetically. I have these in a different table, since I might want to add genres later.

But I don't know how to insert these checked genres into my song_genre table. I've never used arrays before, so I'm lost here.

First I want to UPDATE my table_music and that is working fine. Then I want to INSERT INTO my new table (song_genre) so I get the genre and the song_id in here.

I think that's the right way to do it, so I am able to change the genre later or add or remove one genre if I need that as well one day.

I tried the following: but it doesn't seem to work:

foreach(echo $row_rs_genres['genre_id'] as $checked_value)
{
   $query = "INSERT INTO song_genre (genre_id, song_id) VALUES ('$checked_value',    $_POST['song_id'])"; 
   $result = mysql_query($query) or die ("Unable to execute query: " . mysql_error());

}

  • 写回答

2条回答 默认 最新

  • dsfhe34889 2013-03-30 17:59
    关注

    Remove the echo:

    foreach($row_rs_genres['genre_id'] as $checked_value)
    

    And you need to enclose the $_POST in curly brackets:

    "INSERT INTO song_genre (genre_id, song_id) VALUES ('$checked_value', {$_POST['song_id']})"
    

    Or break out of the string:

    "INSERT INTO song_genre (genre_id, song_id) VALUES ('" . $checked_value . "', " . $_POST['song_id'] . ")"
    

    Although you're susceptible to SQL injection here. You should escape your input or switch to prepared statements.

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示