Welcome Guest, Not a member yet? Register   Sign In
How to store facebook user data into database ?
#1

[eluser]Keloo[/eluser]
Hi I'm working on an ecommerce site and I want my users to login with their facebook accounts, but I want to grab that data and store it into the db using codeigniter.

How should I do it ?

Can anyone help me with a few tips, or links?

I've managed to authenticate using facebook API, but I need somehow to store that user related data into a users table.
#2

[eluser]animatora[/eluser]
Hi, what I would do is get the information back from FB and create a new user account based on it. Then logged them using that account. If you want you can even link more than one FB account to that user. Having the user account you can now log whatever information you need
#3

[eluser]Keloo[/eluser]
Not sure I understand.

I've tried something like this:
Code:
function account()
{
  $fb_data = $this->session->userdata('fb_data');

  if((!$fb_data['uid']) or (!$fb_data['me']))
  {

   redirect('home/index');
  }
  else if($this->db->get('users')->num_rows() < 0){
    $query = array(
    'uid' => $fb_data['uid'],
    'fullname' => $fb_data['me']['name'],
    'email' => $fb_data['me']['email']
   );

    $this->db->insert('users',$query);
   }
   else
   {

     $data = array(
      'fb_data' => $fb_data

    );

    $this->layouts->set_title('My Account');
    $this->layouts->view('account', $data);
   }

  }

It seems that it's working, and I can access the account page.

But is it ok ? How should I improve it ?

I'm trying to get the data, only the first time the user has logged in with facebook and accessed his account.

I don't know how should I make it work in another way.
#4

[eluser]Keloo[/eluser]
I've encountered a problem.


Each time I log in with facebook account, and try to insert the fb data into the db I get an error something like:

Quote:Error Number: 1062

Duplicate entry '[email protected]' for key 'email'

INSERT INTO `users` (`uid`, `fullname`, `email`) VALUES ('xxxxxxxxxx', 'xxxxxxxxxx', '[email protected]')

Filename: C:\wamp\www\ecommerce\system\database\DB_driver.php

Line Number: 330


For instance I was thinking something like this: only the first time the user is loging in with his fb account to grab the data and insert it into the db, else skip this process.

How should I check if the id is already stored into the db ?

What should I do to fix this issue ?

#5

[eluser]animatora[/eluser]
First try to login the user based on the credentials you have received from FB, if the user dosn't exists then create new one and logged him in.
#6

[eluser]Anonymous[/eluser]
[quote author="Keloo" date="1337770467"]Not sure I understand.

I've tried something like this:
Code:
function account()
{
  $fb_data = $this->session->userdata('fb_data');

  if((!$fb_data['uid']) or (!$fb_data['me']))
  {

   redirect('home/index');
  }
  else if($this->db->get('users')->num_rows() < 0){
    $query = array(
    'uid' => $fb_data['uid'],
    'fullname' => $fb_data['me']['name'],
    'email' => $fb_data['me']['email']
   );

    $this->db->insert('users',$query);
   }
   else
   {

     $data = array(
      'fb_data' => $fb_data

    );

    $this->layouts->set_title('My Account');
    $this->layouts->view('account', $data);
   }

  }

It seems that it's working, and I can access the account page.

But is it ok ? How should I improve it ?

I'm trying to get the data, only the first time the user has logged in with facebook and accessed his account.

I don't know how should I make it work in another way.[/quote]

You have made the email column a unique key in your DB and you are trying to insert another entry with the same key.

With $this->db->get('users')->num_rows() < 0 you are just querying the users table for all rows.

Change this:
Code:
else if($this->db->get('users')->num_rows() < 0){

To something that DOES check if the supplied email is already in the database. Eg:

Code:
else if($this->users_model->getUserByEmail($fb_data['me']['email'])->num_rows() < 0){
#7

[eluser]Keloo[/eluser]
I've tried something like this:

in the model I'm checking for the id

Code:
public function id_check($id)
{
  $this->db->where('id', $id);
  return $this->db->get('users')->num_rows();
}

and in the controller have something like this:
Code:
function account()
{
  $fb_data = $this->session->userdata('fb_data');

  if((!$fb_data['uid']) or (!$fb_data['me']))
  {

   redirect('home/index');
  }
  else if($this->Facebook_model->id_check($fb_data['me']['id']) < 0 )
  {
   $query = array(
    'id' => $fb_data['me']['id'],
    'fullname' => $fb_data['me']['name'],
    'email' => $fb_data['me']['email']
   );

   $this->db->insert('users',$query);
  
  }
  else
  {
  
   $data = array(
      'fb_data' => $fb_data

    );

   $this->layouts->set_title('My Account');
   $this->layouts->view('account', $data);
  }

}

The problem is, when I'm logging in with another fb account, it doesn't grab the user data and insert it in the DB, because there is data.

I need to check by id. If that ID already exists, don't insert data, but if it doesn't insert it.





Theme © iAndrew 2016 - Forum software by © MyBB