2013-12-10 56 views
0

我的foreach循環不適用於mysql中的update語句。不知道我做錯了什麼。這裏是我的PHP片段。PHP和MYSQL:更新不適用於Foreach循環

 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循環更新的罰款。我似乎無法找出問題所在。任何幫助表示讚賞。謝謝!

+0

'的print_r($ _ POST)'在php文件中看到柱結構是什麼樣子.. – superphonic

+0

什麼是錯誤,使用一些調試,讓我們知道確切的問題 – Noor

+0

print_r($ _ POST)顯示和空陣列。順便說一句我正在使用angularjs和創建一個YouTube的API應用程序。 – Growlithe

回答

0

您有問題,在您的查詢報價:「。$播放列表 「

$ playlistResult = 'UPDATE video_playlists SET CHANNEL_ID = '[」 CHANNEL_ID] 「 ',PLAYLIST_ID ='」 $播放列表[。 '',playlist_title ='「。$ playlist ['title']。'',playlist_status ='」。$ playlist ['status']。'',playlist_description ='「。$ playlist ['description '','playlist_created ='「。$ playlist ['date_created']。」'「;

你可能會得到語法錯誤: 檢查粗體文字:「‘$播放列表。[‘頻道了’。’」

$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']."\'"; 

我希望這可以幫助,如果需要檢查您的更新狀態爲好。

+0

是的,它確實說,當我直接在數據庫中運行它時有語法錯誤, – Growlithe

+0

**您有錯誤你的SQL語法;檢查與您的MySQL服務器版本相對應的手冊,以便在'$ playlistResult = "'附近使用正確的語法UPDATE video_playlists SET'channel_id' ='"。$ playlist ['channel'at line 1 **我不明白,其他更新與這種語法工作 – Growlithe

0

首先,請重新檢查代碼,它應該是$ _REQUEST []而不是$請求

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

小寫工作正常 – Growlithe