我要直奔主题了。我正在我的本地主机上使用 MAMP 构建一个原型 PHP 应用程序。
我想让最终用户尽可能轻松地进行注册转换。
因此,对于初学者来说,我使用 fb phpsdk 来获取所有用户图形数据以无摩擦地构建用户配置文件。我的一切都显示得很好。
但是,我正在努力让数据使用 PDO 成功存储。在我将其用于下面的示例之前,我成功地使用带有表单输入的 $_POST 方法使代码工作。
我只是不明白为什么没有 $_POST 方法它就不能工作。数据库设置得很好,就像我说我测试它并使用下面的 PDO 代码一样。
使用 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]));?>
有任何想法吗?