Jump to content

loop to add generated key


mforan

Recommended Posts

how would i loop this to ensure that "erg34t3t4errgg" for example, would not appear twice?

 

<?php
# UNIQUE KEY
$letters = range('a', 'z');
shuffle($letters);
$uniqueid = md5(time() . implode('', $letters) . rand());
# ADD KEY TO DATABASE
$query = "INSERT INTO attacks SET id ='$uniqueid',username='$user',attacked='$buildchange[1]'";
mysql_query($query) or die("Error: ".mysql_error());
?>

 

i know the chances are slim, that it would happen. would i use a for function?

Link to comment
https://forums.phpfreaks.com/topic/129809-loop-to-add-generated-key/
Share on other sites

Set your id field in your accounts table to Unique. Now MySQL will prevent duplicate entries of the same unique id being inserted into the id column.

 

So, if I have a table with an ID field.  And I don't want the ID entries to ever be repeated, could I just use the uniqueID function and set the field to Unique?

 

And then, if an entry is ever the same as another one, it will change it automatically for me?

 

If you are trying to create a uniqueID that will never appear twice in a database, I don't think you need to use loops.

Its taxing resources unnecessarily.

 

yer i know what you mean, id use auto increment. trouble is, the way the code is layed out i will have to write the id number, then write the information  later.

 

unless there is a way to pull off the last entry of say username='username' in the table?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.