views:

18

answers:

2

Hi all, i am scrubbing my head now for hours to solve thw following situation:

Several Html Forms on a webpage are identified by an id. Users can create forms on the clients side themselves and fill in data. How can I guarantee that the id of the form the user generates is unique and that there doesnt occure any collision in the saving process because the same id was generated by the client of someone else.

The problems/questions:

  • A random function on the client side could return identical id's on two clients
  • Looking up the SQL table for free id wouldnt solve the problem
  • Autoincrement a new id would complicate the whole process because DOM id and SQL id differ so we come to the next point:
  • A "left join" to combine dom_id and user_id to identify the forms in the database looks like a performance killer because i expect these tables will be huge

The question (formed as simple as i can):

Is there a way that the client can create/fetch a unique id which will be later used as the primary key for a database entry without any collisions? Whats the best practice?

My current solution (bad):

No unique id's at all to identify the forms. Always a combination through a left join to identify the forms generated by the specific user. But what happens if the user says: Delete my account (and my user_id) but leave the data on the server. I would loose the user id and this query qouldn't work anymore...

I am really sorry that i couldn't explain it in another way. But i hope someone understood what i am faced with and could give me at least a hint

THANK YOU VERY MUCH!

+1  A: 

GUIDs (Globally Unique IDentifiers) might help. See http://en.wikipedia.org/wiki/GUID

For each form the client could generate a new GUID. Theoretically it should be unique.

Andrew Cooper
Thanks for the advice... Think i might use that function:http://php.net/manual/en/function.uniqid.php Never heard of it before. But i think using this with a combined prefix: user_database_auo_increment_iteration_index + user_id might be the savest thing.
Bosh
A: 

I just don't show IDs to the user until they've submitted something, at which point they get to see the generated auto-increment id. It keeps things simple. If you however really need it, you could use a sequence table, but it has some caveats which make me advise against it:

CREATE TABLE sequence (id integer default 0, sequencename varchar(32));

Incrementing:

UPDATE sequence 
SET id = @generated := id + 1 
WHERE sequencename = 'yoursequencename';

Getting:

SELECT @generated;
Wrikken