2

可能重复:
无法保存超过 2 条记录

我正在使用 Facebook Javascript SDK 登录并获取用户信息。当我将此数据保存到 mysql 数据库时,它只保存 1-2 条记录。虽然我每次都在获取用户记录。

if($user){
                print_r($userInfo);
                $userid = $userInfo[id];
                $username = $userInfo[name];
                $birthday = $userInfo[birthday];
                $birthday = date("Y-m-d", strtotime($birthday));
                $userlocation = $userInfo[location][name];
                $gender = $userInfo[gender];
                $useremail = $userInfo[email];
                $sql = "INSERT INTO users (userid, username, useremail, birthday, location, gender) VALUES ('$userid', '$username', '$useremail', '$birthday', '$userlocation', '$gender')";
                if($result = mysql_query($sql)) {
                    echo "success";
                }
            }

用户的表定义是:

1   userid  int(20)         No  None          Change      Drop    Browse distinct values     Primary      Unique      Index  Spatial     Fulltext
 2  username    varchar(30) latin1_swedish_ci       No  None          Change      Drop    Browse distinct values      Primary     Unique      Index  Spatial      Fulltext
 3  useremail   varchar(50) latin1_swedish_ci       No  None          Change      Drop    Browse distinct values      Primary     Unique      Index  Spatial      Fulltext
 4  birthday    date            No  None          Change      Drop    Browse distinct values      Primary     Unique      Index  Spatial     Fulltext
 5  location    varchar(30) latin1_swedish_ci       No  None          Change      Drop    Browse distinct values      Primary     Unique      Index  Spatial      Fulltext
 6  gender  varchar(10) latin1_swedish_ci       No  None          Change      Drop    Browse distinct values      Primary     Unique      Index  Spatial      Fulltext

使用 PDO 时:

if($user){
                $host = "internal-db.s130813.gridserver.com";
                $db = "db130813_fbmooz";
                $user = "db130813_adminu";
                $pass = "ansari786";
                $conn = new PDO("mysql:host=$host;dbname=$db",$user,$pass);
                $uid = $userInfo[id];
                $uname = $userInfo[name];
                $sql = "INSERT INTO users (userid,username) VALUES (:uid,:uname)";
                $q = $conn->prepare($sql);
                $q->execute(array(':uid'=>$uid, ':uname'=>$uname));

仍然无法挽救

4

1 回答 1

0

有几个想法浮现在脑海:

  • 使用 PDO 重写 - 'mysql_query' fn 已弃用
  • 使用准备好的语句和“mysql_real_escape_string”来确保值没有任何坏字符
  • 检查返回值并使用 'mysql_error()' 查看是否出现问题
于 2012-09-29T12:33:33.457 回答