/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] My First SQL Code w/ PHP..Repetition Issue

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]they do, and [B]if [/B]I haven’t stored their FB basic info (with the [B][U]name[/U][/B] as a checking point in the row of information), then their FB basic info is added.

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[/U].

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 ( “?>”, “&gt;”, $msg);
$msg = str_replace ( “<?”, “&lt;”, $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’);

?>
[/code]

Are there any suggestions anyone?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@ChuckBauthorAug 29.2010 — ...sorry everyone...I solved it...
×

Success!

Help @ChuckB spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...