PDO数据不存储在db中

时间:2013-10-12 06:36:40

标签: php mysql pdo

我会直言不讳。我正在我的localhost上使用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]));?>

有什么想法吗?

2 个答案:

答案 0 :(得分: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)";

答案 1 :(得分:0)

查看源代码,默认情况下,库似乎使用POST。如果您要使用GET请求,则在调用method方法时必须提供api()参数。我的PHP变得非常生疏,但我会看看我是否能够提出你需要使用的正确语法......

修改

看起来像你想要的东西:

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

这是未经测试的(目前我没有针对PHP的测试环境),但我从项目的Github页面中的测试中提取了这个:https://github.com/facebook/facebook-php-sdk/blob/master/tests/tests.php