簡體   English   中英

PHP腳本具有Facebook數據,但不會將其插入MySQL數據庫

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

我已經為此工作了大約2天,在檢查了我的代碼和錯誤日志數小時后仍找不到解決方案。 因此,我希望新鮮的眼睛會有所幫助。

我正在收集Facebook數據並將其插入數據庫。 當我在$ sql INSERT命令上運行var_dump時,我在字符串中看到了所有facebook數據。 我還在每個變量上運行var_dumps以確保數據在那里。 它以每個字符串類型顯示。 這符合數據庫的期望-VARCHAR有足夠的空間。

我在該數據庫中還有其他一些表,它們仍在接受數據,因此這似乎不是數據庫問題(這是共享服務器,我無權訪問它)。 另外,我已經嘗試了INSERT語句中幾乎所有語法變化,不同引號等的操作,但無濟於事。

最后,由於mysql_error()而導致的錯誤是“您的SQL語法有誤;請查看與您的MySQL服務器版本相對應的手冊,以獲取正確的語法以在附近使用-只是一個冷酷的家伙,正在尋找一些有趣...”,在第1行的“ AAAIAZB21He9sBAOLpbm3XTwabMVX0s”。 您在單引號(')中看到的是INSERT語句中$ fbabout和$ at VALUES的當前數據。

這樣,這是php文件的代碼。 預先感謝您抽出寶貴的時間檢查此信息!

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

正如其他人所評論的那樣,在將任何數據插入數據庫之前,至少應至少使用mysql_real_escape_string()對其進行清理。 就您而言,“我”正在給您帶來麻煩。

我還有另一個查詢; 您通過包含facebook.php來使用PHP SDK,但是您還可以使用file_get_contents手動查詢圖形。 這讓我感到奇怪,因為如果您使用的是SDK,則不需要這樣做。 SDK將創建一個對象,然后您對其進行查詢。

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

上面的鏈接可能會有幫助。

$ table_name瀏覽器將其讀取為變量,將其從table_name中刪除

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM