0

我有一个表单,用于发送多个名称相似的字段,但使用方括号将它们作为数组发送,键为“id”。我能够使用 foreach 循环成功循环,但我的插入查询未能在 db.any 中进行任何更改,为什么有任何线索?

这是表格中的代码:

$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>"; 
}

这是我的表单处理器上的代码

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>";
    }
}
4

1 回答 1

1

尝试这个。

还要注意安全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>";
                }
            }
于 2013-06-17T12:52:56.143 回答