0

I have some code in script that gets some values and posts them to php which (i already know works) should add to the database but doesnt? just wondering whats going wrong, my guess is that its never actually getting to my php file? any ideas?

FB.Event.subscribe('auth.login', function(response) 
{               
    FB.api('/me', function(response) {
        $.post("usersignup.php", { facebookid: response.id, email: response.email, firstname: response.first_name, lastname: response.last } );             
    });   

appropriate part of usersignup.php looks like this

  mysql_select_db ($database);  
    $fbid = $_POST['facebookid'];
    $fn = $_POST['firstname'];
    $ln = $_POST['lastname'];
    $em = $_POST['email'];    
      $query = "INSERT INTO user";
      $query .= "(facebookid, firstname, lastname, email) VALUES ('$fbid','$fn','$ln','$em')";
      $results = mysql_query($query, $link);          
      $mediaid = mysql_insert_id();
6
  • 2
    Don't guess. Add error handling and some output that tracks your script's execution. Commented Apr 2, 2011 at 22:04
  • this is running on your website and not a fb non-iframe canvas, right? Commented Apr 2, 2011 at 22:31
  • yea on my website, user clicks login to facebook, they log in then $.post should be called Commented Apr 2, 2011 at 22:37
  • and you have included the jQuery libs correct? Commented Apr 2, 2011 at 22:59
  • no how do i do this? this could be the problem Commented Apr 2, 2011 at 23:01

3 Answers 3

1

Ok so there may be a php error lets add some error handling...

Modify your PHP:

ini_set('display_errors', 1);

if(!mysql_select_db ($database))
{
   echo 'Could not connect';
   exit;
}
else
{
  $fbid = $_POST['facebookid'];
  $fn = $_POST['firstname'];
  $ln = $_POST['lastname'];
  $em = $_POST['email'];    
  $query = "INSERT INTO user";
  $query .= "(facebookid, firstname, lastname, email) VALUES ('$fbid','$fn','$ln','$em')";

  if(!$results = mysql_query($query, $link))
  {
     echo 'Query failed: '.mysql_error();
     exit;
  }

  $mediaid = mysql_insert_id();
  echo 'Success: '. $mediaid;
}

And your js should alert the response:

FB.Event.subscribe('auth.login', function(response) 
{               
  FB.api('/me', function(response) {
    $.post("usersignup.php", { 
        facebookid: response.id, 
        email: response.email, 
        firstname: response.first_name, 
        lastname: response.last 
      },
      function(rdata){ alert(rdata); }, 
      'text'
    );             
  });
});

Also keep an eye on your JS error console... if there is an issue with your JS its going to tell you.


$.post is a shortcut for $.ajax therefore it doesnt support the full config hash youre giving it. The signature is $.post(url, data, successCallback) if you need more control than this then use $.ajax.

Sign up to request clarification or add additional context in comments.

2 Comments

What should i use to run this, im doing this in dreamweaver and therefore getting no error message
Oh preview it in a browser - id recommend Firefox with Firebug installed... never use the Dreamweaver preview... its useless ;-)
0

I'm guessing you've got an SQL error. Change your line to look like this:

$results = mysql_query($query, $link) or die(mysql_error());

and see if an error is being generated.

I'm betting that one of your values is breaking the query. You need to be running everything through mysql_real_escape_string(), at a minimum.

$fn = mysql_real_escape_string($_POST['firstname']);

1 Comment

No neither fix it. i think it maybe that its not completing $.post but i dont know why, does $.post have to be called?
0

Your PHP file does not create a connection to the MySQL server, and $database is not defined.

2 Comments

it is defined, and created just not in the code i have pasted
@James: Right, so when you said "usersignup.php looks like this", you were lying. Please amend your question accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.