Jump to content

Unique code based on users unique email address inserted into mysql table?


iCeR

Recommended Posts

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();		
    	}
    }

This I wrote for a fellow for non repetative unique id generation. However, the string lenght of the id is abt 9-10. Make so alteration withn da code so as to generate only 5 characters and made it useful for urself :)

 

<?php
$str_alpha="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$str_num=1234567890;
$shuffle_alpha=str_shuffle($str_alpha);
$shuffle_num=str_shuffle($str_num);
$alpha=str_split($shuffle_alpha,7);
$num=str_split($shuffle_num,3);
$rand_alpha=mt_rand(0,6);
$rand_num=mt_rand(0,2);
$rand_alpha_output=$alpha[$rand_alpha];
$rand_num_output=$num[$rand_num];
$join_output=$rand_alpha_output.$rand_num_output;
if(file_exists('printed_id.txt'))
{
        $getID=file_get_contents('printed_id.txt');
        $printed_id=explode(PHP_EOL,$getID);    
        while($final_output=str_shuffle($join_output))
        {       
                if(!in_array($final_output,$printed_id))
                {
                       
                        $final_output=str_shuffle($join_output);//valid id for further usage and processing
                        break;
                }
        }
       echo $final_output; $fileOPN=fopen('printed_id.txt','a+');
        fwrite($fileOPN,$final_output);
        fwrite($fileOPN,"\r\n");        
}
else
{
        $final_output=str_shuffle($join_output);
        $fileOPN=fopen('printed_id.txt','a+');
        fwrite($fileOPN,$final_output);
        fwrite($fileOPN,"\r\n");
        echo $final_output;//valid id for further usage and processing
}

?>

Thanks spaceman12.

 

Pretty much don't require what's below:

 

$join_output=$rand_alpha_output.$rand_num_output;

 

Using the generated $join_output, how can I check if that has been already generated in the DB with another user, and also assign the value of $join_output to the column for that email address of the user.

m giving u some ideas as how to accomplish the result u desire. As is in ur case, a lighter code is only required. Join output adds up together the alpha and nume strings.

 

While loops in da code ensures that by all means, any value inserted in da db is unique with

100 percent guarantee! XD

Sorry, I still don't understand. It's all fairly new to me, which is why I asked for assistance with the code relating to my application and question :)

Thanks nevertheless - I hope someone can help with editing my code and explaining what I have to do where so I can learn from it.

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.