这是我的程序可以正常工作并获得用户兴趣,即正确的电影列表。 现在我想插入数据库,所以我写了一个ajax调用来做到这一点,
有人可以告诉我它有什么问题,它不会给出错误但也不会插入表格。
我将数据发布到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 :(得分: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>