Facebook PHP SDK- Storing user data in MySql database not working -
i have been using fb sdk can user login fb account, during phase want user data stored on db , after following multiple solutions on doesn't seem work. data printing out variable proving there nothing wrong that, think there wrong sql insert statement cant seem see it.
my code main page given below:
<?php // path php-sdk include('config.php'); require '......./src/facebook.php '; $facebook = new facebook(array( 'appid' => '', 'secret' => '', )); /* $facebook = new facebook(array( 'appid' => appinfo::appid(), 'secret' => appinfo::appsecret(), 'sharedsession' => true, 'trustforwarded' => true, )); */ // see if there user cookie ## connect mysql server $mysqli = new mysqli(db_server, db_username, db_password, db_database); # check connection if ($mysqli->connect_errno) { echo "<p>mysql error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>"; exit();} $user = $facebook->getuser(); if ($user) { try { // proceed knowing have logged in user who's authenticated. $user_profile = $facebook->api('/me'); // $fbid = $user_profile['id']; // facebook id $fbuname = $mysqli->real_escape_string($user_profile['name']); // facebook username $fbfname = $mysqli->real_escape_string($user_profile['first_name']); // facebook full name $fblname = $mysqli->real_escape_string($user_profile['last_name']); // facebook full name $fbemail = $mysqli->real_escape_string($user_profile['email']); // facebook email id } catch (facebookapiexception $e) { echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>'; $user = null; } } ?> <!doctype html> <html xmlns:fb="http://www.facebook.com/2008/fbml"> <head> <title>facebook application local development</title> <link type="text/css" rel="stylesheet" href="css/reset.css"> <link type="text/css" rel="stylesheet" href="css/main.css"> </head> <body> <div id="login"> <?php if (!$user) { ?> <fb:login-button></fb:login-button> <?php } ?> </div> <div id="cont"> <!--redirect local welcome page?!!--> <?php if ($user): // $result = $mysqli->query("select username users username = '$fbuname' limit 1"); // check if getting in result //print "<pre>"; //print_r($result); //print "</pre>"; $result = $mysqli->query("select username users username = '{$fbuname}' limit 1"); print "<pre>"; print_r($result); print "</pre>"; if ($result->num_rows == 0){ // check if enters loop echo "true"; echo "<br/>" ; $sql= mysqli_query($db, "insert users (username,first_name,last_name,email) values ('{$user_profile['name']}', '{$user_profile['first_name']}', '{$user_profile['last_name']}', '{$user_profile['email']}')"); //$sql= mysqli_query($db,$sql); if ( $sql === false ){ //handled sql error here //echo "error"; die($mysqli->error); } } echo "<br/>" ; echo $fbuname; echo $fbfname; echo $fblname; echo $fbemail; //header("location: welcome.php"); //<!--img src="https://graph.facebook.com/ <//?php echo $user; picture"--> //<!--p hello // echo $user_profile['name']; !</p--> else: echo "you not connected."; endif ?>
why code not working
you're mixing procedural , object oriented styles. plus, $db not contain credentials database connection it's empty.
solution
replace
$sql= mysqli_query($db, "insert users (username,first_name,last_name,email) values ('{$user_profile['name']}', '{$user_profile['first_name']}', '{$user_profile['last_name']}', '{$user_profile['email']}')");
with
$sql= $mysqli->query("insert users (username,first_name,last_name,email) values ('{$user_profile['name']}', '{$user_profile['first_name']}', '{$user_profile['last_name']}', '{$user_profile['email']}')");
Comments
Post a Comment