Hello…
I am new to the MySQL world. I just wrote my first piece of code that happens to be an integration with a Facebook application.
When a certain page loads, the program looks to see if someone has a Facebook connection.[B] If [B]if [B][U]name
If they are connected, and I’ve stored their information, but I don’t have an email stored, and I now have access to an email, then I just want to update the [U]email column for that particular name
The problem I’m having is that when someone loads the page, I’m getting multiple rows of the same information in the database. It probably a small fix.
Here’s the Code:
[code=php]
<?php
require ‘lib/facebook.php’;
require ‘config.php’;
function format_msg($msg){
$msg = str_replace ( “<iframe>”, ” “, $msg);
$msg = str_replace ( “</iframe>”, ” “, $msg);
$msg = str_replace ( “<script”, ” “, $msg);
$msg = str_replace ( “script>”, ” “, $msg);
$msg = str_replace ( “<form>”, ” “, $msg);
$msg = str_replace ( “</form>”, ” “, $msg);
$msg = str_replace ( “?>”, “>”, $msg);
$msg = str_replace ( “<?”, “<”, $msg);
$msg = str_replace ( “‘”, “’”, $msg);
$msg = str_replace ( ‘”‘, ‘”‘, $msg);
$msg = str_replace ( “n”, “<br>”, $msg);
return $msg;
}
$facebook = new Facebook(array(
‘appId’ => $appid,
‘secret’ => $appsecret,
‘cookie’ => true,
));
$session = $facebook->getSession();
$fbme = null;
// Session based API call.
if ($session) {
try {
$fbme = $facebook->api(‘/me’);
$uid = $fbme[id];
} catch (FacebookApiException $e) {
error_log($e);
}
}
$DB=”fbsadata”;
$linkID = @mysql_connect(“fbsadata.db.5481533.hostedresource.com”, “fbsadata”, “SOC_4_life”);
mysql_select_db($DB, $linkID);
// login or logout url will be needed depending on current user state.
if (!$fbme) {
$loginUrl = $facebook->getLoginUrl(array(
‘fbconnect’ => 0,
‘req_perms’ => ’email’,
‘display’ => ‘popup’,
‘next’ => ‘http://www.sociallyaffluent.com/closewindow.php’,
‘cancel_url’ => ‘http://www.sociallyaffluent.com/closewindow.php’ ));
} else {
$user_name = $fbme[‘name’];
$user_hometown = $fbme[‘hometown’][‘name’];
$user_current_loc = $fbme[‘location’][‘name’];
$user_school = $fbme[‘education’][0][‘school’][‘name’];
$user_grad_year = $fbme[‘education’][0][‘year’][‘name’];
$user_gender = $fbme[‘gender’];
$user_email = $fbme[’email’];
$user_bio = format_msg($fbme[‘bio’]);
$check_user = “SELECT * FROM facebook_info WHERE name =’$user_name'”;
$check_user_result = mysql_query($check_user, $linkID);
$user_count = mysql_num_rows($check_user_result);
if ($email_count > 0){
//if found
$check_user_email = “SELECT user_email FROM facebook_info WHERE name = ‘$user_name'”;
mysql_query($check_user_email, $linkID);
if ( (!isset($check_user_email)) && (isset($user_email)) ) {
$sql=”UPDATE facebook_info SET user_email = ‘$user_email’ WHERE name = ‘$user_name'”;
mysql_query($sql, $linkID);
mysql_close($linkID);
break;
}
} else {
//did not find one
$sql=”INSERT INTO facebook_info (name, hometown, location, education, grad_year, gender, user_email, user_bio) VALUES(‘$user_name’,’$user_hometown’,’$user_current_loc’,’$user_school’,’$user_grad_year’,’$user_gender’,’$user_email’,’$user_bio’)”;
mysql_query($sql, $linkID);
mysql_close($linkID);
}
}
// This call will always work since we are fetching public data.
$naitik = $facebook->api(‘/naitik’);
?>
Are there any suggestions anyone?