2013-09-26 44 views
1

這是我的程序,它可以很好地工作並正確地獲得用戶的興趣,即電影列表。 現在我想插入到數據庫中,所以我寫了ajax調用來做到這一點,在數據庫中使用ajax/jquery插入facebook電影列表

有人可以告訴我它有什麼問題,它不會給出錯誤,但也不會插入到表中。

我張貼的數據movies_db.php

的index.php

<!DOCTYPE html> 
<html xmlns:fb="https://www.facebook.com/2008/fbml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
     <title>ThenWat</title> 
    </head> 
    <body> 
     <div id="fb-root"></div> 
     <script type="text/javascript"> 
      var button; 
      var userInfo; 
      window.fbAsyncInit = function() { 
       FB.init({ appId: '862', 
        status: true, 
        cookie: true, 
        xfbml: true, 
        oauth: true}); 

       showLoader(true); 

       function updateButton(response) { 
        button  = document.getElementById('fb-auth'); 
        userInfo  = document.getElementById('user-info'); 

        if (response.authResponse) { 
         //user is already logged in and connected 
         FB.api('/me?fields=id,name,movies,email', function(info) { 
         console.log(info.movies); 
          login(response, info); 
          var json = JSON.stringify(myinfo.movies.data); 
        var a = JSON.parse(json);  

         $.post('movies_db.php',{'myd':a}, function(data) 
        { 
         $.ajax({ 
        url:'url.php' 
        ,async:  true 
        ,cache:  false 
        ,dataType: 'html' 
        ,success: function(data){ 
         $('body').html(data); 
          } 
        });   
         }); 

         }); 

         button.onclick = function() { 
          FB.logout(function(response) { 
           logout(response); 
          }); 
         }; 
        } else { 
         //user is not connected to your app or logged out 
         button.innerHTML = 'Login'; 
         button.onclick = function() { 
          showLoader(true); 
          FB.login(function(response) { 
           if (response.authResponse) { 
            FB.api('/me', function(info) { 

             login(response, info); 
            }); 
           } else { 
            //user cancelled login or did not grant authorization 
            showLoader(false); 
           } 
          }, {scope:'email,user_birthday,status_update,user_about_me'}); 
         } 
        } 
       } 

       // run once with current status and whenever the status changes 
       FB.getLoginStatus(updateButton); 
       FB.Event.subscribe('auth.statusChange', updateButton); 
      }; 
      (function() { 
       var e = document.createElement('script'); e.async = true; 
       e.src = document.location.protocol 
        + '//connect.facebook.net/en_US/all.js'; 
       document.getElementById('fb-root').appendChild(e); 
      }()); 

      function login(response, info){ 

        if (response.authResponse) { 
        var accessToken         = response.authResponse.accessToken; 

        userInfo.innerHTML        = '<img src="https://graph.facebook.com/' + info.id + '/picture">' + info.name   
        button.innerHTML        = 'Logout'; 
        showLoader(false); 
        document.getElementById('other').style.display = "block"; 
       } 
      } 

      function logout(response){ 
       userInfo.innerHTML        = ""; 
       document.getElementById('debug').innerHTML  = ""; 
       document.getElementById('other').style.display = "none"; 
       showLoader(false); 
      } 

      function showLoader(status){ 
       if (status) 
        document.getElementById('loader').style.display = 'block'; 
       else 
        document.getElementById('loader').style.display = 'none'; 
      } 
     </script> 
     <button id="fb-auth">Login</button> 
     <div id="loader" style="display:none"> 
      <img src="ajax-loader.gif" alt="loading" /> 
     </div> 
     <br /> 
     <div id="user-info"></div> 
     <br /> 
     <div id="debug"></div> 
    </body> 
</html> 

movies_db.php 這部分是正確的,我相信

<?php 

$con =mysqli_connect('127.0.0.1:3306', 'root', 'root', 'test'); 
if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: ".mysqli_connect_error(); 
} 

foreach($_POST['myd'] as $elem){ 
    echo $elem['name']; 
    echo $elem['id']; 
    echo '<br/>'; 

    $ID=$elem['id']; 
    $Name=$elem['name']; 

    $sql = "INSERT INTO interest (movies,id) VALUES('".$Name."','".$ID."')"; 
      if (!mysqli_query($con,$sql)) 
      { 
        die('Error: ' . mysqli_error($con)); 
      } 
} 
?> 
+0

你有沒有試過在'movies_db.php'中檢查'$ _POST ['myd']'的值? – Reigel

+0

參數化您的查詢或容易受到SQL注入 – allen213

+0

@Reigel:我該怎麼辦?我在movies_db.php上執行了'echo $ _POST ['myd']',它在頁面上甚至沒有顯示任何內容,甚至沒有'echo'嗨「 – user123

回答

0

在這裏,我已經添加用戶其它規範信息,以同樣的方式可以存儲他的電影興趣。

<script type = "text/javascript"> 
     var userid; 
     FB.init({ 
     //appId: '', 
     appId: 'fb id', 
     xfbml: true, 
     status: true, 
     cookie: true, 
     }); 
     FB.getLoginStatus(checkLoginStatus); 
     function authUser() 
     { 
      FB.login(checkLoginStatus, {scope:'email'}); 
     } 
     function checkLoginStatus(response) 
     {    
      if(response && response.status == 'connected') 
      { 
      FB.api('/me?fields = movies,email,name,gender,locale,location,link', function(mydata) 
      { 
       console.log(mydata.email); 
       console.log(mydata.id); 
       userid = mydata.id; 
       var name = mydata.name; 
       gender = mydata.gender; 
       locale = mydata.locale; 
       city = mydata.location; 
       link = mydata.link; 
       //alert(name); 
       var email = mydata.email; 
       //var json = JSON.stringify(mydata.movies.data); 
       //var a = JSON.parse(json); 
       var picture = "https://graph.facebook.com/"+userid+"/picture?type = small"; 
       // alert(picture); 
       $.post('user_record.php',{ name: name, email: email, userid:userid, picture:picture, gender: gender, locale: locale, city: city, link: link}, function(data) 
       { 
        var $form = $("<form id = 'form1' method = 'post' action = 'start.php'></form>"); 
        $form.append('<input type = "hidden" name = "userid" value = "'+userid+'" />'); 
        $('body').append($form); 
        window.form1.submit(); 
       }); 
      }); 

      console.log('Access Token: ' + response.authResponse.accessToken); 
      } 
      else 
      { 
       document.getElementById('loginButton').style.display = 'block'; 
      } 
     } 
    </script>