Jump to content


Photo

More of a SQL question...kind of


  • Please log in to reply
2 replies to this topic

#1 pgsjoe

pgsjoe
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 26 March 2006 - 04:36 AM

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....
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);

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.

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 26 March 2006 - 11:53 AM

[!--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) View Post[/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?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 jellystones

jellystones
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 09 August 2006 - 07:46 PM



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.


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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users