Jump to content

More of a SQL question...kind of


pgsjoe

Recommended Posts

So I decided I want the ID #'s assigned to each row in my database to be off the wall confusing just to make sure nobody would be able to guess other ones. Here's why...a person enters an event into the database, they get an e-mail back with a link to edit the event that is reference by the ID. So obviously using IDs as simple as 32, 33, 34 some jerk could easily mess with all listings.

So I found this code....
[code]settype($template, "string");

// you could repeat the alphabet to get more randomness
$template = "1234567890abcdefghijklmnopqrstuvwxyz";

function GetRandomString($length) {
       global $template;
       settype($length, "integer");
       settype($rndstring, "string");
       settype($a, "integer");
       settype($b, "integer");
             for ($a = 0; $a <= $length; $a++) {
               $b = rand(0, strlen($template) - 1);
               $rndstring .= $template[$b];
       }
             return $rndstring;
      }

   $id = GetRandomString(30);[/code]

And it works beautifully! Get the id as: vzda0brfh10by64qoby25m440n99jhj

Now, it feeds it into the database, even displays it back on the page. But, for some reason, it won't allow me to edit it. The "edit" process I'm running through worked for, and still works for any IDs set as a couple digits. I changed my primary key from INT to VARCHAR since it wouldn't accept TEXT as a primary.

Just seems weird that it will read and register in the database, but not allow me to edit it. So, wonder if anyone has any ideas why this would happen or if there's a better way.

Oh, one more question. I was contemplating using the rand() function, because it says it goes up to 32,000 or something, and I know I won't need more than that. But, if using it as a primary, do I have to worry about it duplicating numbers and then not allowing my customers to post?

Thanks in advance.
Link to comment
Share on other sites

[!--quoteo(post=358460:date=Mar 26 2006, 05:36 AM:name=PGS JoE)--][div class=\'quotetop\']QUOTE(PGS JoE @ Mar 26 2006, 05:36 AM) [snapback]358460[/snapback][/div][div class=\'quotemain\'][!--quotec--]
But, if using it as a primary, do I have to worry about it duplicating numbers and then not allowing my customers to post?

Thanks in advance.
[/quote]

Yes. PK must be unique.

Having allocated and emailed ID to user, why would you want to edit it?
Link to comment
Share on other sites

  • 4 months later...
[quote author=pgsjoe link=topic=89344.msg358460#msg358460 date=1143347760]


Oh, one more question. I was contemplating using the rand() function, because it says it goes up to 32,000 or something, and I know I won't need more than that. But, if using it as a primary, do I have to worry about it duplicating numbers and then not allowing my customers to post?

Thanks in advance.
[/quote]

You have to worry about it duplicating numbers in both cases. However with the first case the chance of a duplicate will be much more rare.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.