SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Random ID's

  1. #1
    HI silver trophy Silverado4x4's Avatar
    Join Date
    Jan 2001
    Location
    USA
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Random ID's

    How can I make my script check to see if there is already an ID there that it has randomly generated, and if there is one, to pick another one, but if not use that one? (to avoid duplicates)

    PHP Code:
    <?
    include("cards.php");
    function 
    CreateID($length=16)
    {
            
    $Pool "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            
    $Pool .= "1234567890digitalmyth";

            for(
    $index 0$index $length$index++)
            {
                    
    $sid .= substr($Pool,
                            (
    rand()%(strlen($Pool))), 1);
            }

            return(
    $sid);
    }

    $sid=CreateID();
    mysql_connect(localhost,$user,$pass);
    @
    mysql_select_db($db) or die( "Unable to select database");
       
    $result mysql_query("INSERT INTO sentcards VALUES('$sid', '$s_name', '$s_email','$r_name', '$r_email','$message', '$image', '$age', '$location', '$work', '$drive', '$hair', '$eyes', '$morf')");
            if(!
    $result
            {
            
    $sid=CreateID();

            
    $result mysql_query("INSERT INTO sentcards VALUES('$sid', '$s_name', '$s_email','$r_name', '$r_email','$message', '$image', '$age', '$location', '$work', '$drive', '$hair', '$eyes', '$morf')");
            }
    ?>
    <HTML>
    <HEAD>
    <TITLE>Redirecting, please wait!</TITLE>
    <meta http-equiv="refresh" content="0; URL=http://www.jokeuniversity.com/webpsychic/web.php?ID=<? echo "$sid"?>">
    </HEAD>
    <BODY>
    </BODY>
    </HTML>

  2. #2
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put your create sid in a while loop

    $createdOk = FALSE;

    do while ($createOK == FALSE)
    {
    here.... create sid query
    if ($result) then $creatOK = TRUE to exit loop
    }

    and of course, SID should be defined as UNIQUE or as primary key in your table.

  3. #3
    HI silver trophy Silverado4x4's Avatar
    Join Date
    Jan 2001
    Location
    USA
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it's only a primary key, then no two will be alike?

  4. #4
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's right. The primary key is unique. You can test it, and see.

  5. #5
    HI silver trophy Silverado4x4's Avatar
    Join Date
    Jan 2001
    Location
    USA
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •