Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4062366
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:35:21+00:00 2026-05-20T15:35:21+00:00

I have a mysql table which will store users email addresses (each is unique

  • 0

I have a mysql table which will store users email addresses (each is unique and is the primary field) and a timestamp.
I have added another column called 'unique_code' (varchar(64), utf8_unicode_ci).

What I would very much appreciate assistance with is;

a) Generating a 5 digit alphanumeric code, ie: 5ABH6
b) Check all rows the ‘unique_code’ column to ensure it is unique, otherwise re-generate and check again
c) Insert the uniquely generated 5 digit alphanumeric code into 'unique_code' column, corresponding to the email address just entered.
d) display the code on screen.

What code must I put and where?

My current php is as follows:

require "includes/connect.php";

$msg = '';

if($_POST['email']){

    // Requested with AJAX:
    $ajax = ($_SERVER['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest');

    try{
        if(!filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)){
            throw new Exception('Invalid Email!');
        }

        $mysqli->query("INSERT INTO coming_soon_emails
                        SET email='".$mysqli->real_escape_string($_POST['email'])."'");

        if($mysqli->affected_rows != 1){
            throw new Exception('You are already on the notification list.');
        }

        if($ajax){
            die('{"status":1}');
        }

        $msg = "Thank you!";

    }
    catch (Exception $e){

        if($ajax){
            die(json_encode(array('error'=>$e->getMessage())));
        }

        $msg = $e->getMessage();        
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:35:22+00:00Added an answer on May 20, 2026 at 3:35 pm

    Hope this helps:

    a) I did something very similar to this where I was generating unique codes which were to be used as URLs. I wrote this to generate the codes:

    private function _generateCode($length = 5) {
    
        $characters = 'bcdfghjkmnpqrstvwxyz';
    
        $string = '';
        for ($i = 0; $i < $length; $i++) {
            $string .= $characters[rand(0, strlen($characters) - 1)];
        }
    
        return $string;
    
    }
    

    $characters is a string of “allowed” characters. We chose to remove the vowels so that there was no chance of making unwanted words 🙂 You could change this. There are simpler ways to write it but we needed something quite specific.

    You would use it like this:

    $unique_code = _generateCode();
    

    b) For this just wrap your insert statement in a select statement check for that unique code. If the code exists then generate another code and try again. One way you could do this is (NB: this isn’t tested and it may be susceptible to an infinite loop if you happen to get into a situation where you’ve used up all your codes 😉 And you should probably add a check to make sure the INSERT was succesful):

    $unique_code = "";
    $inserted = false;
    // Keep looping until we've inserted a record
    while(!$inserted) {
        // Generate a code
        $unique_code = _generateCode();
        // Check if it exists
        if ($result = mysqli->query("SELECT unique_code FROM coming_soon_emails WHERE unique_code = '$unique_code'")) {
            // Check no record exists
            if ($result->num_rows == 0) {
                // Create new record
                $mysqli->query("INSERT INTO coming_soon_emails (email,unique_code) VALUES ('" . $mysqli->real_escape_string($_POST['email']) . "','$unique_code')");
                // Set inserted to true to ext loop
                $inserted = true;
                // Close the result object
                $result->close();
            }
        } else {
            // Quit if we can't check the database
            die('Something went wrong with select');
        }   
    }
    
    // Output the code
    echo $unique_code;
    

    c) To insert the unique code just add this to your insert statement where $unique_code is the variable assigned the return value from the function above:

    $mysqli->query("INSERT INTO coming_soon_emails (email,unique_code) VALUES ('".$mysqli->real_escape_string($_POST['email'])."','$unique_code')");
    

    d) Just echo the variable you assigned the code to e.g:

    echo $unique_code;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table, in which I will be storing the email_id of
if i have a table which has columns with fixed lenght, Will mySQL count
I have a MySQL table which has a product_id field (big integer) 1102330008 1102330025
I have a mysql table field set as time type which stores data in
I have the following PHP function which will list the users from a MySql
I have a MySQL database which currently uses a tinyint field to store the
I have a MYSQL-Table which stores scores. Every time a user improves a new
I have a mysql table which columns are post_id and post_msg. Now I want
I have a huge MySQL table which has its rows encoded in UTF-8 twice.
I have a MySql table called reviews which stores a lot of product reviews,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.