【问题标题】:Set default username and concatenate with ID设置默认用户名并与 ID 连接
【发布时间】:2018-07-09 15:07:18
【问题描述】:

使用下面的代码,我怎样才能在我的数据库中添加一个默认值为“user”的用户名,然后是“id”字段,例如第 15 个用户它变成“user15”?

我不确定在哪里添加默认用户名,我也不是 100% 的语法。

澄清:我想要一个静态字符串(“用户”)并将其与数据库中自动递增的“id”字段连接起来。

更新:似乎不可能立即连接自动递增的值,所以:关于如何生成默认用户名的任何建议?用户名字段是唯一的。

    $firstname = $profile['first_name'];
    $lastname = $profile['last_name'];
    $email = $profile['email'];
    $picture = $profile['picture']['url'];

    try {
        $conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare("
            INSERT INTO users (
                firstname, 
                lastname, 
                email, 
                picture, 
                token
            ) 
            VALUES (
                :firstname, 
                :lastname, 
                :email, 
                :picture, 
                :token)"
            );
        $stmt->bindParam(':firstname', $firstname);
        $stmt->bindParam(':lastname', $lastname);
        $stmt->bindParam(':email', $email);
        $stmt->bindParam(':picture', $picture);
        $stmt->bindParam(':token', $accessToken);

        $stmt->execute();
        echo "Thank you for registering";
    }
    catch(PDOException $e)
    {
        echo "Error: " . $e->getMessage();
    }
    $conn = null;

} else {

    $loginUrl = $helper->getLoginUrl('mywebsite.com/fblogin/index.php', $permissions);
    echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
}

【问题讨论】:

  • 虽然不能保证它是唯一的,但uniqid() 可能是一个好的开始。如果您没有数百万用户,您可能永远不会遇到冲突。
  • UUID 也是一个选项。重复的机会大约是 0.00000000006 (6 × 10−11)。
  • 您可以插入一条空白/null/列的默认值为用户名的记录,然后调用插入 ID,然后更新用户名。

标签: php mysql concatenation


【解决方案1】:

我在评论中提出了这个建议,但让我尝试一个答案来充实它。基本上这个想法是插入一条新记录,然后获取插入 ID,然后更新有问题的记录。

if (something_supposed_to_happen_here()) {
    $firstname = $profile['first_name'];
    $lastname = $profile['last_name'];
    $email = $profile['email'];
    $picture = $profile['picture']['url'];

    try {
        $conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare("
            INSERT INTO users (
                firstname, 
                lastname, 
                email, 
                picture, 
                token
            ) 
            VALUES (
                :firstname, 
                :lastname, 
                :email, 
                :picture, 
                :token)"
            );
        $stmt->bindParam(':firstname', $firstname);
        $stmt->bindParam(':lastname', $lastname);
        $stmt->bindParam(':email', $email);
        $stmt->bindParam(':picture', $picture);
        $stmt->bindParam(':token', $accessToken);

        $stmt->execute();

        // Suggestion follows
        $insertId = $conn->lastInsertId();
        $stmt = $conn->prepare('UPDATE users SET username = :username WHERE id = :id');
        $userId = 'user' . $insertId;
        $stmt->bindParam(':username', $userId);
        $stmt->bindParam(':id', $insertId);
        $stmt->execute();
        // End suggestion

        echo "Thank you for registering";
    }
    catch(PDOException $e)
    {
        echo "Error: " . $e->getMessage();
    }
    $conn = null;

} else {

    $loginUrl = $helper->getLoginUrl('mywebsite.com/fblogin/index.php', $permissions);
    echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
}

请注意,这假定您的自动递增 ID 字段名为 id,用户名字段名为 username

【讨论】:

  • 正确,我的 ID 字段称为“id”。添加了您的建议,该建议返回:“致命错误:未捕获的错误:无法通过 /home/username/mydomain/fblogin/index.php:141 中的引用传递参数 2:堆栈跟踪:#0 {main} throw in /home/username/第 141 行的 mydomain/fblogin/index.php 因为这是 Facebook 登录,所以我通过将字符串“user_”与 Facebook 字段“id”(Facebook 用户 ID)连接起来来修复它。相信这是独一无二的。
  • 如果您只有 Facebook 用户注册,它将足够独特。正如一些 cmets 建议的那样,您可以使用uniqid() 或类似的方法来生成几乎肯定是唯一的 ID。错误出现在$stmt-&gt;bindParam(':username', 'user' . $insertId); 行中 - 该值必须通过引用传递,即是指向内存的变量。我已经编辑了帖子以反映这一点。
【解决方案2】:

只需与 Facebook 的用户 ID 连接即可解决:

$firstname = $profile['first_name'];
$lastname = $profile['last_name'];
$email = $profile['email'];
$picture = $profile['picture']['url'];
$userstatus = "1";
$fbuserid = $profile['id'];
$defaultUsername = 'user_' . $fbuserid; // This line solved it. 

//<editor-fold desc="> Insert the data above this into the database">
try {
    $conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("INSERT INTO users (firstname, lastname, email, picture, status, token, oauth_uid) 
VALUES (:firstname, :lastname, :email, :picture, :userstatus, :token, :oauth_uid)");
    $stmt->bindParam(':firstname', $firstname);
    $stmt->bindParam(':lastname', $lastname);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':picture', $picture);
    $stmt->bindParam(':userstatus', $userstatus);
    $stmt->bindParam(':token', $fbuserid); // just oauth_uid stored in a better named column, also storing that with the line below
    $stmt->bindParam(':oauth_uid', $fbuserid); // this is the original of this and the one above

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-18
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多