2013-10-12 51 views
0

我要直截了當地說到。我在本地主機上使用MAMP構建了一個原型PHP應用程序。PDO數據沒有存儲在db

我想讓註冊轉換對最終用戶儘可能輕鬆。

因此,對於初學者,我使用fb phpsdk獲取所有用戶的圖形數據以無縫地構建用戶配置文件。我已經全部顯示正常。

但是,我拉着頭髮試圖讓數據成功地使用PDO存儲。在我之前使用$ _POST方法和表單輸入成功使用它之後,我成功地獲得了代碼。

我只是不明白爲什麼它不工作沒有$ _POST方法。數據庫設置正常,就像我說過的那樣,我測試了它並使用了下面的PDO代碼。

存儲到DB嘗試使用PDO

require_once 'fbphpsdk/src/facebook.php'; // include the facebook php sdk 
$facebook = new Facebook(array(
     'appId' => 'xxxx', // app id 
     'secret' => 'xxxx')// app secret 
); 
$user = $facebook->getUser(); 
if ($user) { // check if current user is authenticated 
    try { 
     // Proceed knowing you have a logged in user who's authenticated. 
     $user_profile = $facebook->api('/me'); 
    } catch (Exception $e) {} 
} 

$user = "xxxx"; 
$pwd = "xxxx"; 

$odb = new PDO("mysql:host=localhost;dbname=xxxx;", $user, $pwd); 

$q = "INSERT INTO users(u_fullname, age, u_location, u_gender, u_bday, u_employer, u_job) VALUES(:u_fullname, :age, :u_location, :u_firstname, :u_gender, :u_bday, :u_employer, :u_job)"; 
$query = $odb->prepare($q); 
$results = $query->execute(array(
     ":u_fullname" => $u_fullname, 
     ":age" => $age, 
     ":u_location" => $u_location, 
     ":u_gender" => $u_gender, 
     ":u_bday" => $u_bday, 
     ":u_employer" => $u_employer, 
     ":u_job" => $u_job)); 

在FB用戶數據(PHP)

$fbuid = $user_profile['id']; 
$u_employer = $user_profile['work'][0]['employer']['name']; 
$u_job = $user_profile['work'][0]['position']['name']; 
$u_firstname = $user_profile['first_name']; 
$u_fullname = $user_profile['name']; 
$u_location = $user_profile['location']['name']; 
$u_gender = ucfirst($user_profile['gender']); 
$u_bday = $user_profile['birthday']; 

//explode the date to get month, day and year 
$u_bday = explode("/", $u_bday); 

//get age from date or birthdate 
$age = (date("md", date("U", mktime(0, 0, 0, $u_bday[0], $u_bday[1], $u_bday[2]))) > date("md") ? ((date("Y")-$u_bday[2])-1):(date("Y")-$u_bday[2]));?> 

任何想法?

+0

哪裏user_profile'獲取填充'$? –

+0

我有一個外部的php文件(通過php require加載)看到我上面的編輯看代碼 –

+0

假設你在本地執行此操作,如果你使用'print_r($ user)',你會得到什麼? –

回答

1

查詢是錯誤的,更換

你有一個額外的:u_firstname在查詢

$q = "INSERT INTO users(u_fullname, age, u_location, u_gender, u_bday, u_employer, u_job) VALUES(:u_fullname, :age, :u_location, :u_firstname, :u_gender, :u_bday, :u_employer, :u_job)";

$q = "INSERT INTO users(u_fullname, age, u_location, u_gender, u_bday, u_employer, u_job) VALUES(:u_fullname, :age, :u_location, :u_gender, :u_bday, :u_employer, :u_job)";

+0

修復它,它仍然不存儲:( –

0

源代碼來看,似乎圖書館默認使用POST。如果您想要使用GET請求,則在調用api()方法時必須提供method參數。我的PHP越來越漂亮生疏,但我會看看我能拿出你需要使用正確的語法...

編輯:

看起來像你想要的東西,如:

$user_profile = $facebook->api(
    '/me', 
    array('method' => 'GET') 
); 

這是未經測試(我沒有在時刻PHP測試環境),但我把從該項目的Github的頁面測試:https://github.com/facebook/facebook-php-sdk/blob/master/tests/tests.php

+0

有趣。我將它添加到我的php文件中,但它似乎並沒有使差異變得很大:(還有其他想法嗎?我想也許這是因爲我抓住的所有fb圖形數據都是從fb傳遞過來的數組到php變量? –

+0

我試過了一切,顯然這是不可能的。 我覺得我的機率是更好的使用FQL抓取用戶數據。 –

+0

@AndrewSquire我懷疑這是不可能的,但我很遺憾不夠熟悉看起來我問了上面的錯誤問題;你從print_r($ user_profile)得到了什麼? –