I am developing a project on PHP and MySQL. My problem is "I am unable to show a uniqueID in a textbox like 'GHY00001, GHY00002, etc' auto-incremented everytime i load the page and insert it into the database". I already tried some php tutorials but didn't find usuable to my project. Please help me. I am new to PHP and MySQL so please give me the full code. Thanx in advance.

Dani AI

Generated

Short version: if you want IDs that look like GHY00001, stop trying to "encrypt" a random token and use a numeric sequence plus formatting. ’s current random-string approach produces unreadable tokens; is right that encryption is unnecessary for an incrementing display code. The usual, safe pattern is: let MySQL give you an AUTO_INCREMENT number, then format it as GHY + zero-padded number and save or show that formatted string.

Typical flow (safe and simple):

  1. Insert the row without a formatted code (let MySQL assign id).
  2. Read LAST_INSERT_ID() (in PHP: $mysqli->insert_id or PDO::lastInsertId()).
  3. Build the code: GHY + str_pad($id, 5, '0', STR_PAD_LEFT').
  4. Update the row (or store the formatted code in a separate column) and show it in the textbox.

Example (PHP + mysqli, after an insert):

$id = $mysqli->insert_id; // numeric AUTO_INCREMENT
$code = 'GHY' . str_pad($id, 5, '0', STR_PAD_LEFT);
$update = $mysqli->prepare("UPDATE items SET code = ? WHERE id = ?");
$update->bind_param('si', $code, $id);
$update->execute();

If you absolutely must display the future code before the user submits the full form, use a tiny sequence table to reserve a number (atomic INSERT into an AUTO_INCREMENT table) and convert that into GHYxxxxx. That reserves a unique number without SELECT MAX race conditions:

$mysqli->query("INSERT INTO seq_gen () VALUES ()");
$seq = $mysqli->insert_id;
$code = 'GHY' . str_pad($seq, 5, '0', STR_PAD_LEFT);

Notes and cautions: always enforce a UNIQUE index on the formatted code, handle duplicate-key errors (retry), use InnoDB/transactions for multi-step flows, and remember sequential IDs are guessable — don’t use them as secure tokens. If showing an ID before insert isn’t required, prefer the post-insert approach for simplicity and safety.

Recommended Answers

All 3 Replies

Show your script please

<?PHP 
//set the random id length 
$random_id_length = 8; 

//generate a random id encrypt it and store it in $rnd_id 
$rnd_id = crypt(uniqid(rand(),1)); 

//to remove any slashes that might have come 
$rnd_id = strip_tags(stripslashes($rnd_id)); 

//Removing any . or / and reversing the string 
$rnd_id = str_replace(".","",$rnd_id); 
$rnd_id = strrev(str_replace("/","",$rnd_id)); 

//finally I take the first 10 characters from the $rnd_id 
$rnd_id = substr($rnd_id,0,$random_id_length); 

echo "Random Id: $rnd_id"; 

?> 

I uses this code to create the random uniqueID but it comes in a format like "mX7I1u9i, ANjLrVXn" after every refresh. I want my unique id in "GHY00001, GHY0002". I have attached the picture to the form. Only Unique ID is auto-generated and rest of the fields will be filled by the user. Hope this will give you some idea. 4173f6a160f160db41d726a36a277de2

I don't understand why you want to encrypt a unique ID

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.