A good practice for creating human-typable, non-sequential unique id's

前端 未结 4 612
猫巷女王i
猫巷女王i 2020-12-16 01:21

I need to create a unique id for our users. I don\'t want to use an auto_incrementing id, because I don\'t want users to be able to guess how many users we have, or what the

4条回答
  •  粉色の甜心
    2020-12-16 01:30

    If I understand you correctly, you are trying to let users choose their own unique ID from a table where there is not a user registered already. If this is the case then I would have my User table with userID and userName columns. Then return the userIDs where userName is still null.

    $query = "SELECT userID, userName FROM User WHERE userName=''";
    $result=mysql_query($query);
    while ($row = mysql_fetch_assoc($result)) {
    echo $row['userID'];
    }
    

    If you want them to choose their own then run a query that has a where clause which will check whether a specific, user entered, number has a name. You can set the form up by using $_POST...

    and then your checkID.php

    $userName = $_POST['userName'];
    $userID = $_POST ['userID'];
    

    and then your query

    $query = "SELECT userID, userName FROM User WHERE userName='' AND userID=$userID";
    $result=mysql_query($query);
    
    if ($result) {
    echo "number is available";
    $insert = "UPDATE User
    SET userName=$userName
    WHERE userID=$userID";
    
    $inserted=mysql_query($insert);
    if ($inserted) {
    echo "you have been inserted as: ";
    echo $userID;
    echo $userName;;
    }
    
    
    }
    

    Well I see this as being the basics for you. Obviously you will need to run some validation and error checks.

    Hope this is what you're after. Let me know if not and will come back with something else.

提交回复
热议问题