iCeR Posted March 15, 2011 Share Posted March 15, 2011 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(); } } Quote Link to comment https://forums.phpfreaks.com/topic/230679-unique-code-based-on-users-unique-email-address-inserted-into-mysql-table/ Share on other sites More sharing options...
spaceman12 Posted March 15, 2011 Share Posted March 15, 2011 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/230679-unique-code-based-on-users-unique-email-address-inserted-into-mysql-table/#findComment-1187680 Share on other sites More sharing options...
iCeR Posted March 15, 2011 Author Share Posted March 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230679-unique-code-based-on-users-unique-email-address-inserted-into-mysql-table/#findComment-1187681 Share on other sites More sharing options...
spaceman12 Posted March 15, 2011 Share Posted March 15, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/230679-unique-code-based-on-users-unique-email-address-inserted-into-mysql-table/#findComment-1187682 Share on other sites More sharing options...
spaceman12 Posted March 15, 2011 Share Posted March 15, 2011 FINAL OUTPUT is the generated unique id in either of the conditional case implemented within da code! Quote Link to comment https://forums.phpfreaks.com/topic/230679-unique-code-based-on-users-unique-email-address-inserted-into-mysql-table/#findComment-1187683 Share on other sites More sharing options...
iCeR Posted March 15, 2011 Author Share Posted March 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230679-unique-code-based-on-users-unique-email-address-inserted-into-mysql-table/#findComment-1187684 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.