mysql table structure question (userlogin and userlogin_fb)
i have two tables which form part of my login/register system:
these are userlogin and userlogin_fb
userlogin deals with incoming website login/registration
userlogin_fb deals with incoming facebook connect login/first time registration
more details @ http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/
if a user however uses facebook connect to login - the system checks whether they have been registered onto the database and if not it creates a new record for the user:
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');
}
upon site registration however if the same username/email address exists then it is not possible for that user to register (and create duplicate records)
//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;
now the problem i am experiencing is this. whenever the user registers via the website and uses facebook connect to login i have found out that a duplicate record is created regardless in the userlogin_fb table.
开发者_如何学Goas i don't want to have any duplicate usernames/email addresses across the two tables as well as "two different user login modes" each containing different sets of information pertaining to the user how can i integrate both?
preferably when the user logs in using facebook connect i would run a query checking the userlogin table to see if a duplicate username exists first of all and using those details
if a record exists with the same username/email in userlogin then a new record is not made in userlogin_fb.
but if they do not exist i can create a new record for it.
then if the user tried to register an account using the same username/email i would redirect them to a feature "merge" both accounts.
but this is just a rough idea. please i would appreciate further insight into this question.
hopefully i have worded it well enough!
Facebook update their user id, you want BIGINT(15), although as stated by Sascha Galley VARCHAR is futureproof.
I have 3 tables: site's users, facebook user's and joint facebook-site users.
Data is stored independently in the first two tables, i.e. the same e-mail can appear once in the first two tables, it doesn't matter, they can sign in with which ever one they want.
If the serveur detectes user and fb user, and the persons wishes to associate the two accounts a new row is created in the third table and account infomation is combined in this table (post counts, comments...).
I also had a similar problem. You should use varchar(15) or bigint for the facebook ID.
Is a facebook ID a string or an integer?
What should be the leagth of int in a sql table where i store the facebook profile id?
I would recommend varchar, since it is futureproof and won't slow down your system.
精彩评论