1,105,344 Community Members

Auto Generate key

Member Avatar
kiLLer.zoh_1
Light Poster
32 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
<?php 
function autogen()
{
     $qr="Select * from autogen where gid = 1";
     $we=mysql_query($qr)or die(mysql_error());
      while($brec=mysql_fetch_array($we))
      {
        $ln=$brec['autoln'];
      }            
      $lw= ++$ln;
      return $lw;
}
$newlead=autogen();
$qry="Select * from leads";
$r=mysql_query($qry) or die(mysql_error());
 while($re=mysql_fetch_array($r))
 {
    if($rec['leadno']==$newlead)
    {
        ++$newlead;
    }
 }
$er="UPDATE autogen SET autoln='$newlead'WHERE gid =1";
$bn=mysql_query($er)or die(mysql_error());
?>

This Code generates the key like LEA4545

but this code have some flaws same id could be inserted in table

although i have set in fuction in same found incre ent in it..

and the second flaw is this if i delete the keys ..then the new one created key must be from deleted keys

Member Avatar
broj1
Posting Virtuoso
1,576 posts since Jan 2011
Reputation Points: 220 [?]
Q&As Helped to Solve: 218 [?]
Skill Endorsements: 21 [?]
Featured
 
0
 

Let me repeat to see if I understood:

You want to generate unique keys yourself (IOW mysql autoincrement is not OK for your purpose). What is the format of the key and what are the rules for constructing the key?

then the new one created key must be from deleted keys

If this is the requirement then you have to keep the list of the deleted keys. If you keep it in a database table then just query for first available key.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article