php - mysql表结构问题(用户登录和用户登录fb)

标签 php mysql

我有两个表构成我的登录/注册系统的一部分:

这些是 userloginuserlogin_fb

userlogin 处理传入的网站登录/注册

enter image description here

userlogin_fb 处理传入的 facebook 连接登录/首次注册

enter image description here

更多详情@ http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/

如果用户使用 facebook 连接登录 - 系统会检查他们是否已在数据库中注册,如果没有,它会为用户创建一条新记录:

if ($user){
// We have an active session, let's check if we have already registered the user
$query = mysql_query("SELECT * FROM userlogin_fb WHERE oauth_provider = 'facebook' AND oauth_uid = ".$user_profile['id']);
$result = mysql_fetch_array($query);
// If not, let's add it to the database
if(empty($result)){
    $query = mysql_query("INSERT INTO userlogin_fb (oauth_provider, oauth_uid, username, email) VALUES ('facebook', {$user_profile['id']}, '{$user_profile['username']}', '{$user_profile['email']}')");  
    }
        } elseif(isset($_SESSION['uid'])){
"";
}else{
   // Anonymous user
header('Location: index.php');
}

但是,在网站注册时,如果存在相同的用户名/电子邮件地址,则该用户无法注册(并创建重复记录)

//select all rows from our userlogin,userlogin_fb table where the emails match
$query = sprintf("SELECT 1 
                    FROM userlogin 
                   WHERE `email` = '%s'
                  UNION ALL
                  SELECT 1
                    FROM userlogin_fb 
                   WHERE `email` = '%s' ",
                   $email, $email);
$res1 = mysql_query($query);
$num1 = mysql_num_rows($res1);
//if the number of matches is 1
if($num1 >= 1) {
  //the email address supplied is taken so display error message
  echo '<p class="c7">The <b>e-mail</b> address you supplied is already taken. Please go <a href="register.php">back</a> and provide a new one.<br><img src="resources/img/spacer.gif" alt="" width="1" height="15"></p>';
  include_once ("resources/php/footer.php");        
  exit; 
} else      

//select all rows from our userlogin,userlogin_fb table where the usernames match
$query2 = sprintf("SELECT 1 
                    FROM userlogin 
                   WHERE `username` = '%s'
                  UNION ALL
                  SELECT 1
                    FROM userlogin_fb 
                   WHERE `username` = '%s' ",
                   $username, $username);
$res2 = mysql_query($query2);
$num2 = mysql_num_rows($res2);
//if the number of matches is 1
if($num2 >= 1) {
  //the username supplied is taken so display error message
  echo '<p class="c7">The <b>username</b> you supplied is already taken. Please go <a href="register.php">back</a> and provide a new one.<br><img src="resources/img/spacer.gif" alt="" width="1" height="15"></p>';
  include_once ("resources/php/footer.php");        
  exit;

现在我遇到的问题是这个。每当用户通过网站注册并使用 facebook 连接登录时,我发现无论在 userlogin_fb 表中都会创建重复记录。

因为我不想在两个表中有任何重复的用户名/电子邮件地址以及“两种不同的用户登录模式”,每种模式都包含与用户有关的不同信息集,我该如何整合两者?

最好是当用户使用 facebook connect 登录时,我会运行一个查询来检查 userlogin 表,首先查看是否存在重复的用户名并使用这些详细信息

如果 userlogin 中存在具有相同用户名/电子邮件的记录,则不会在 userlogin_fb 中创建新记录。

但如果它们不存在,我可以为其创建一个新记录。

然后,如果用户尝试使用相同的用户名/电子邮件注册帐户,我会将他们重定向到“合并”两个帐户的功能。

但这只是一个粗略的想法。请进一步深入了解这个问题。

希望我的措辞足够好!

最佳答案

Facebook 更新他们的用户 ID,您需要 BIGINT(15),尽管如 Sascha Galley 所说,VARCHAR 是面向 future 的。

我有 3 个表:站点用户、facebook 用户和 facebook 站点联合用户。

数据独立存储在前两个表中,即同一封邮件可以在前两个表中出现一次,没关系,他们可以用任何一个登录。

如果服务器检测到用户和 fb 用户,并且人们希望将这两个帐户相关联,则会在第三个表中创建一个新行,并将帐户信息组合在此表中(帖子计数、评论...)。

关于php - mysql表结构问题(用户登录和用户登录fb),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6249927/

相关文章:

MySQL JOIN+WHERE 没有给出结果

javascript - 从javascript或jquery中的另一个页面重定向时如何自动单击按钮?

php - 基于存储在 Json 文件中的元数据创建数据库模式的 PHP 项目的名称?

Mysql 具有 NAND 操作和 NOT 操作的多个查询

mysql - 对另一个字段的不同值进行分组求和

php - SQL 优化左外连接查询

php - MySQL数据库复制方法

php - 在 PHP 中隐藏/显示列表

php - 如何使用特定订单进行更新

mysql - my.cnf 根据配置优化