开发者

Won't add to database using script and php

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();


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.


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']);


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜