Page 1 of 1

Insert into pdo working halfway

Posted: Fri Oct 12, 2012 11:15 am
by francoboy7
Hi everyone
this script http://pastebin.com/9LwMEPHi
(php code at the end of post if you prefer

Uses hyridatuh (a class to connect to social media such as facebook google and twitter) and get their user data such as name, email, gender.

My problem is that the script if working very well when data comes from facebook or Twitter.
It does check if datas exist in database if so echo it exists, if not insert datas in the database.

Problem is when provider of data is Google, the script seems to insert the data and echoing the inserted data confirmation, but it is not. No data from google are inserted in database.

Do you guys see any mistakes in the code ( :? )

PS : basic script can be tested here
http://www.entendu.info/social/

If you have any question, comment or suggestion to render my code better, feel free

php code :

Code: Select all

<?php
        session_start();
       
        // config and includes
        $config = dirname(__FILE__) . '/../social/hybridauth/config.php';
    require_once( "../social/hybridauth/Hybrid/Auth.php" );
 
       
   // the selected provider
 
   $provider_name = $_GET["provider"];
 
   try{
       // initialize Hybrid_Auth with a given file
       $hybridauth = new Hybrid_Auth( $config );
 
       // try to authenticate with the selected provider
  $adapter = $hybridauth->authenticate( $provider_name );
       
       // then grab the user profile
       $user_profile = $adapter->getUserProfile();
 
 
if (isset($user_profile->email))
        {
$email = $user_profile->email;
        }
else
        {
$email = "verifyme@yopmail.com";
        }
$username = $user_profile->displayName;
 
$password = $user_profile->identifier;
 
        $password = md5($password);
 
$first_name = $user_profile->firstName;
 
if (isset($user_profile->lastName))
        {
$last_name = $user_profile->lastName;
        }
else
        {
$last_name = "Twitter";
        }
if (isset($user_profile->gender))
        {
        $gender = $user_profile->gender;
        }
else
        {
        $gender = "male";
        }
 
$identifier = $user_profile->identifier;
 
 
 
try
{
    $pdo = new PDO('mysql:host=localhost;dbname=*****', '****', '************');
 
         //on tente d'exécuter les requêtes suivantes dans une transactions
 
    //on lance la transaction
 
   $req = $pdo->prepare('SELECT username FROM members WHERE email = :email OR identifier = :identifier');
        $req->execute(array('email' => $email, 'identifier' => $identifier));
 
                if($req->rowCount()) {
                        echo 'il existe un utilisateur';
                }
               
                else {
        $req = $pdo->prepare('INSERT INTO members SET email = :email, username = :username, password = :password, firstname = :firstname, lastname = :lastname, gender = :gender, identifier = :identifier, provider = :provider');
        $req->execute(array('email' => $email, 'username' => $username, 'password' => $password, 'firstname' => $first_name, 'lastname' => $last_name, 'gender' => $gender, 'identifier' => $identifier, 'provider' => $provider_name));
                        echo 'Vous etes maintenant inscrit';
 
                       
                }
 
       
}
 
catch(Exception $c) //en cas d'erreur
{
   
    //on affiche un message d'erreur ainsi que les erreurs
    echo 'Tout ne s\'est pas bien passé, voir les erreurs ci-dessous<br />';
    echo 'Erreur : '.$c->getMessage().'<br />';
    echo 'N° : '.$c->getCode();
 
    //on arrête l'exécution s'il y a du code après
    exit();
}
 
 
 
  }
   catch( Exception $e ){
       echo "Error: please try again!";
       echo "Original error message: " . $e->getMessage();
   }
   
   ?>
   
   
<fieldset>
    <legend>Or use anohter service</legend>
 
    <a href="?provider=facebook">Facebook</a><br />
    <a href="?provider=twitter" >Twitter</a><br />
        <a href="?provider=google" >Google</a><br />
 
</fieldset>
 
<fieldset>
        <legend>Info</legend>
       
        <?php
echo $email;
echo "<br />";
echo $username;
echo "<br />";
echo $last_name;
echo "<br />";
echo $first_name;
echo "<br />";
echo $gender;
echo "<br />";
echo $identifier;
echo "<br />";
echo $user_profile->birthday;
 
 
        ?>
 
       
</fieldset>

Re: Insert into pdo working halfway

Posted: Sat Oct 13, 2012 10:49 pm
by Eric!
Most likely the Hybrid_Auth is not passing the data you are expecting. Have you looked into what you are getting back when accessing Google and double checked the SQL strings? Perhaps there is a character in there or something causing problems.

Re: Insert into pdo working halfway

Posted: Sun Oct 14, 2012 12:36 am
by francoboy7
Solved, problem was that my field username in the database was set a primary key, so I couldn't write another entry with same username.

PS : Is there an intelligent way to be able to register different people with different email but same username (which is First and Last name)

2 people called John Smith but not same email, how will they be diferenciated by other users ?