简体   繁体   English

PHP脚本具有Facebook数据,但不会将其插入MySQL数据库

[英]php script has facebook data but won't insert it into mysql database

I've been working on this for ~2 days now and can't find a solution after checking my code and error logs for hours. 我已经为此工作了大约2天,在检查了我的代码和错误日志数小时后仍找不到解决方案。 So, I'm hoping some fresh eyes will help. 因此,我希望新鲜的眼睛会有所帮助。

I'm gathering facebook data and inserting it into my database. 我正在收集Facebook数据并将其插入数据库。 When I run a var_dump on the $sql INSERT command I see all the facebook data in the string. 当我在$ sql INSERT命令上运行var_dump时,我在字符串中看到了所有facebook数据。 I've also run var_dumps on each variable to make sure the data is there. 我还在每个变量上运行var_dumps以确保数据在那里。 It is and each show as a string type. 它以每个字符串类型显示。 This matches what the database is expecting--VARCHAR with plenty of room. 这符合数据库的期望-VARCHAR有足够的空间。

I have a few other tables in this database and they are still accepting data so it doesn't seem to be a database issue (this is a shared server and I don't have access to it). 我在该数据库中还有其他一些表,它们仍在接受数据,因此这似乎不是数据库问题(这是共享服务器,我无权访问它)。 Also, I've tried what seems like almost every variation of syntax, different quotes, etc. in the INSERT statement, but to no avail.... 另外,我已经尝试了INSERT语句中几乎所有语法变化,不同引号等的操作,但无济于事。

Finally, the error I get as a result of mysql_error() is "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'm just a chill dude looking for some fun . . .','AAAIAZB21He9sBAOLpbm3XTwabMVX0s' at line 1". 最后,由于mysql_error()而导致的错误是“您的SQL语法有误;请查看与您的MySQL服务器版本相对应的手册,以获取正确的语法以在附近使用-只是一个冷酷的家伙,正在寻找一些有趣...”,在第1行的“ AAAIAZB21He9sBAOLpbm3XTwabMVX0s”。 What you are seeing in the single quotes (') is the current data for the $fbabout and $at VALUES in the INSERT statement. 您在单引号(')中看到的是INSERT语句中$ fbabout和$ at VALUES的当前数据。

With that, here is the code to the php file. 这样,这是php文件的代码。 Thank you in advance for taking the time to check this out! 预先感谢您抽出宝贵的时间检查此信息!

<?php 
require_once("facebook.php");

$app_id = "";
$app_secret = "";
$my_url = "";



$code = $_POST["code"];

if(empty($code)) {
        $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
        . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
        . $_SESSION['state']."&scope=email,user_birthday,user_interests,friends_interests,publish_stream,user_about_me,user_checkins";

        echo("<script> top.location.href='" . $dialog_url . "'</script>");



} else {
    $host=""; // Host name 
    $username=""; // Mysql username 
    $password=""; // Mysql password 
    $db_name=""; // Database name 
    $tbl_name=""; // Table name  

    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");  

        $token_url = "https://graph.facebook.com/oauth/access_token?"
        . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
        . "&client_secret=" . $app_secret . "&code=" . $code;

        $response = file_get_contents($token_url);
        $params = null;
        parse_str($response, $params);

        $_SESSION['access_token'] = $params['access_token'];

        $graph_url = "https://graph.facebook.com/me?access_token=". $params['access_token'];
    $interests = "https://graph.facebook.com/me/interests?access_token=". $params['access_token'];

    $user = json_decode(file_get_contents($graph_url));
    $user_interests = json_decode(file_get_contents($interests));

    $id = $user->id;        
    $fname = $user->first_name;
    $lname = $user->last_name;
    $link = $user->link;
    $gender = $user->gender;
    $locale = $user->location->name;
    $email = $user->email;
    $bday = $user->birthday;

    $uidata = array();
    $number = count($user_interests->data);
            for ($i = 0;$i<=$number-1;$i++){
           array_push($uidata,($user_interests->data[$i]->name));
    }
    $ui = implode(",", $uidata);

    $fbabout = $user->bio;
    $at = $params['access_token'];

    // Insert data into mysql 
    $sql="INSERT INTO $tbl_name(fbid,fname,lname,link,gender,locale,email,birthday,interests,fbabout,fbtoken)VALUES('".$id."','".$fname."','".$lname."','".$link."','".$gender."','".$locale."','".$email."','".$bday."','".$ui."','".$fbabout."','".$at."')";
    $result=mysql_query($sql);


    if($result) {
        header( 'Location: https://crushonit.com/join/fbRegister.html' );
    } else {
        echo mysql_error();
    }

} 
?>

<?php 
// close connection 
mysql_close();
?>

As other people have commented, you should always clean any data before inserting it into a database, using mysql_real_escape_string() at the very least. 正如其他人所评论的那样,在将任何数据插入数据库之前,至少应至少使用mysql_real_escape_string()对其进行清理。 In your case, the "I'm" is causing you problems. 就您而言,“我”正在给您带来麻烦。

I have another query though; 我还有另一个查询; you are using the PHP SDK, by including facebook.php, but you are also manually querying the graph using file_get_contents. 您通过包含facebook.php来使用PHP SDK,但是您还可以使用file_get_contents手动查询图形。 This strikes me as odd, because if you are using the SDK you don't need to do that. 这让我感到奇怪,因为如果您使用的是SDK,则不需要这样做。 The SDK will create an object and you query that. SDK将创建一个对象,然后您对其进行查询。

https://developers.facebook.com/docs/reference/php/ https://developers.facebook.com/docs/reference/php/

The above link may be of some help. 上面的链接可能会有帮助。

$ table_name浏览器将其读取为变量,将其从table_name中删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM