1

在阅读了很多之后,最后我的 fb 应用程序正在做我所期望的(收集数据并将其存储在我的数据库中)。但是,如果用户返回页面选项卡中的应用程序(用户已登录应用程序),则表中的条目将重复。正如我在教程中阅读的那样,我的代码中有一部分用于验证用户是否已经在数据库中,如果这是一个新用户,那么它将记录数据,如果不是什么都没有。

请看一下我的代码并指导我解决它。谢谢

我没有收到任何错误,但似乎问题可能出在此处:

 $query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND  oauth_uid = ". $user_profile['id']);  
      $result = mysql_fetch_array($query);  

这是我的代码:

 <?php
 define('db_user','xxx');
 define('db_password','xxx');
 define('db_host','xxx.xxx.com');
 define('db_name','xxx');

 // Connect to MySQL
 $dbc = @mysql_connect (db_host, db_user, db_password) OR die ('Could not connect to   MySQL: ' . mysql_error() );
// Select the correct database
mysql_select_db (db_name) OR die ('Could not select the database: ' . mysql_error() );
mysql_set_charset('utf8',$dbc);

require 'src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId'  => 'xxx',
'secret' => 'xxx',
  'cookie' => true));


 // Get User ID
 $user = $facebook->getUser();

 // We may or may not have this data based on whether the user is logged in.
 //
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$fbuid = $facebook->getUser();
 $user_profile = $facebook->api('/me');



   } catch (FacebookApiException $e) {
   error_log($e);
   $user = null;
   }
     }else{
   header('Location: index.php');

    }
    //know if the user is in the db 
      $query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND oauth_uid = ". $user_profile['id']);  
      $result = mysql_fetch_array($query);  


    if(empty($result)){ 
      $query = mysql_query("INSERT INTO users (oauth_provider, oauth_uid, username, first_name, last_name, birthday, email, pic_square ) VALUES    ('facebook', {$user_profile['id']}, '{$user_profile['name']}', '{$user_profile['first_name']}','{$user_profile['last_name']}','{$user_profile['birthday']}','{$user_profile['email']}', '{$user_profile['pic_square']}')");  
  $query = mysql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());  
  $result = mysql_fetch_array($query) or die ( mysql_error () );  


}  


   // Login or logout url will be needed depending on current user state.
    if ($user) {
     $paramsout = array('next'=>'http://www.mywebsite.com/test/logout.php');
     $logoutUrl = $facebook->getLogoutUrl($paramsout);
     }
     ?>
     <html><body>welcome</body></html>
4

0 回答 0