duanjiaopi8218 2013-06-17 12:44
浏览 14

插入具有相似名称的多个字段

I have a form I'm using to send multiple fields with a similar name but using square brackets to send them as an array with the key being 'id'. I am able to loop through successfully using a foreach loop but my insert query fails to make any changes in the db.any clues as to why?

here is the code from the form:

$artist = mysql_fetch_array($allartists);
$allmusic = get_music_for_artist($artist_id);
$id = $music['id'];

while ($music = mysql_fetch_array($allmusic)) {
    echo "<td class=\"td20 tdblue\">
        <input name=\"song_title[" . $id . "]\" type=\"text\" value=\"" . $music['song_title'] . "\" />
        </td>"; 
}

and here is the code on my form processor

foreach ($_POST['song_title'] as $id => $song) {
    $query = "UPDATE music SET 
                song_title = '{$song}' 
                WHERE id = $id ";
    if (mysql_query($query, $connection)) {
        //Success
        header("Location: yourmusic.php?pid=3");
        exit;
    } else {
        //Display error message
        echo "update did not succeed";
        echo "<p>" . mysql_error() . "</p>";
    }
}
  • 写回答

1条回答 默认 最新

  • douzhe9927 2013-06-17 12:52
    关注

    Try this.

    Also watch for security http://www.phptherightway.com/#data_filtering

    foreach ($_POST['song_title'] as $id => $song) {
                        $id = (int) $id;
                        $song = mysql_real_escape_string($song);
    
                        $query = "UPDATE music SET 
                        song_title = '$song' 
                        WHERE id = $id LIMIT 1;";
    
                        if (mysql_query($query, $connection)) {
                        //Success
                        header("Location: yourmusic.php?pid=3");
                        exit;
                    } else {
                        //Display error message
                        echo "update did not succeed";
                        echo "<p>" . mysql_error() . "</p>";
                    }
                }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集