我一直在使用FB SDK,以便我可以让用户通过他们的FB帐户登录,但是在这个阶段我希望他们的用户数据存储在我的数据库中,甚至在关注SO之后的多个解决方案之后它也没有。似乎工作。 此外,数据是从变量中打印出来的,从而证明这没有任何问题,说实话,我认为我的SQL INSERT语句有问题,但我似乎无法看到它。
我的主页代码如下:
<?php
// Path to PHP-SDK
include('config.php');
require '......./src/facebook.php ';
$facebook = new Facebook(array(
'appId' => '',
'secret' => '',
));
/* $facebook = new Facebook(array(
'appId' => AppInfo::appID(),
'secret' => AppInfo::appSecret(),
'sharedSession' => true,
'trustForwarded' => true,
)); */
// See if there is a user from a cookie
## connect mysql server
$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
# check connection
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
exit();}
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
// $fbid = $user_profile['id']; // To Get Facebook ID
$fbuname = $mysqli->real_escape_string($user_profile['name']); // To Get Facebook Username
$fbfname = $mysqli->real_escape_string($user_profile['first_name']); // To Get Facebook full name
$fblname = $mysqli->real_escape_string($user_profile['last_name']); // To Get Facebook full name
$fbemail = $mysqli->real_escape_string($user_profile['email']); // To Get Facebook email ID
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>Facebook Application Local Development</title>
<link type="text/css" rel="stylesheet" href="css/reset.css">
<link type="text/css" rel="stylesheet" href="css/main.css">
</head>
<body>
<div id="login">
<?php if (!$user) { ?>
<fb:login-button></fb:login-button>
<?php } ?>
</div>
<div id="cont">
<!--redirect to local welcome page?!!-->
<?php if ($user):
// $result = $mysqli->query("SELECT username from users WHERE username = '$fbuname' LIMIT 1");
// to check if its getting anything in result
//print "<pre>";
//print_r($result);
//print "</pre>";
$result = $mysqli->query("SELECT username from users WHERE username = '{$fbuname}' LIMIT 1");
print "<pre>";
print_r($result);
print "</pre>";
if ($result->num_rows == 0){
// to check if it enters a loop
echo "true";
echo "<br/>" ;
$sql= mysqli_query($db, "INSERT INTO users (username,first_name,last_name,email)
VALUES ('{$user_profile['name']}', '{$user_profile['first_name']}', '{$user_profile['last_name']}', '{$user_profile['email']}')");
//$sql= mysqli_query($db,$sql);
if ( $sql === false ){
//handled the SQL error here
//echo "ERROR";
die($mysqli->error);
}
}
echo "<br/>" ;
echo $fbuname; echo $fbfname; echo $fblname; echo $fbemail;
//header("location: welcome.php");
//<!--img src="https://graph.facebook.com/ <//?php echo $user; picture"-->
//<!--p Hello // echo $user_profile['name']; !</p-->
else:
echo "You are not Connected.";
endif ?>
答案 0 :(得分:0)
您正在混合程序和面向对象的样式。另外,$ db不包含数据库连接的凭据,因为它是空的。
替换
$sql= mysqli_query($db, "INSERT INTO users (username,first_name,last_name,email)
VALUES ('{$user_profile['name']}', '{$user_profile['first_name']}', '{$user_profile['last_name']}', '{$user_profile['email']}')");
带
$sql= $mysqli->query("INSERT INTO users (username,first_name,last_name,email)
VALUES ('{$user_profile['name']}', '{$user_profile['first_name']}', '{$user_profile['last_name']}', '{$user_profile['email']}')");
答案 1 :(得分:0)
$ db未声明连接值。
将值声明如下......
$db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die("Error " . mysqli_error($db);
,然后才能执行查询。
$sql= mysqli_query($db, "INSERT INTO users (username,first_name,last_name,email)
VALUES ('{$user_profile['name']}', '{$user_profile['first_name']}', '{$user_profile['last_name']}', '{$user_profile['email']}')");