PHP& MYSQL:更新不与Foreach循环一起使用

时间:2013-12-10 11:55:51

标签: javascript php mysql mysqli

我的foreach循环不适用于mysql中的update语句。不知道我做错了什么。这是我的php片段。

        foreach ($request['playlist_information'] as $playlist) {
        $playlistResult = "UPDATE video_playlists SET channel_id='".$playlist['channel_id']."', playlist_id='".$playlist['id']."', playlist_title='".$playlist['title']."', playlist_status='".$playlist['status']."', playlist_description='".$playlist['description']."', playlist_created='".$playlist['date_created']."'";
        $mysqli->query($playlistResult);
        var_dump($playlist['title']);
    }

我的请求来自我的javascript就是这样:

        var param = {
    "channel_id": channelId,
    "video_id": videoId,
    "views": $scope.stats[0]['views'],
    "likes": $scope.stats[0]['likes'],
    "dislikes": $scope.stats[0]['dislikes'],
    "favorites": $scope.stats[0]['favorites'],
    "comments": $scope.stats[0]['comments'],
    "duration": $scope.stats[0]['duration'],
    "dimension": $scope.stats[0]['dimension'],
    "definition": $scope.stats[0]['definition'],
    "caption": $scope.stats[0]['caption'],
    "licensed_content": licensedContent,
    "upload_status": $scope.stats[0]['uploadStatus'],
    "privacy_status": $scope.stats[0]['privacyStatus'],
    "license": $scope.stats[0]['license'],
    "embeddable": embeddable,
    "public_stats_viewable": publicStatsViewable,
    "video_title": $scope.meta[0]['title'],
    "video_status": $scope.stats[0]['privacyStatus'],
    "video_description": $scope.meta[0]['description'],
    "video_tags": $scope.meta[0]['tags'],
    "playlist_information": {}
}



for (var i = 0; i < $scope.playlists.length; i++) {

    param["playlist_information"][i] = {
                    "channel_id": channelId,
                    "id": $scope.playlists[i]['id'],
                    "title": $scope.playlists[i]['title'],
                    "description": $scope.playlists[i]['description'],
                    "status": $scope.playlists[i]['status'],
                    "date_created": $scope.playlists[i]['datePublished']
    }

};

INSERT工作正常,我可以在没有php部分的foreach循环的情况下更新。我似乎无法确定问题所在。任何帮助表示赞赏。谢谢!

2 个答案:

答案 0 :(得分:0)

您的查询中的引号有问题:

$ playlistResult =“UPDATE video_playlists SET channel_id = '”。$ playlist ['channel_id']。“',playlist_id ='”。$ playlist ['id']。“',playlist_title ='“。$ playlist ['title']。”',playlist_status ='“。$ playlist ['status']。”',playlist_description ='“。$ playlist ['description']。”',playlist_created =' ”。$播放列表[ 'DATE_CREATED']。 “'”;

您可能会收到语法错误: 检查粗体文字:'“。$ playlist ['channel_id']。”''

  $playlistResult = "UPDATE video_playlists SET channel_id=\'".$playlist['channel_id']."\', playlist_id=\'".$playlist['id']."\', playlist_title=\'".$playlist['title']."\', playlist_status=\'".$playlist['status']."\', playlist_description=\'".$playlist['description']."\', playlist_created=\'".$playlist['date_created']."\'";

我希望这有助于在必要时检查您的更新条件。

答案 1 :(得分:0)

首先请重新检查代码,应该是$ _REQUEST []而不是$ request

 foreach ($_REQUEST['playlist_information'] as $playlist) {}