We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,434 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Auto Generate key

<?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

2
Contributors
1
Reply
1 Hour
Discussion Span
3 Months Ago
Last Updated
14
Views
kiLLer.zoh_1
Newbie Poster
20 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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.

broj1
Nearly a Posting Virtuoso
1,216 posts since Jan 2011
Reputation Points: 167
Solved Threads: 165
Skill Endorsements: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0687 seconds using 2.67MB