What if we would like to have a unique random number? The previous example does not give us unique numbers. In an online licensing solution, contract numbers generated need to be unique to be stored in the database. So in this case, a new script is needed to execute the following flow:
To implement this, the form.htm will now look as follows: <html> <head> <title>Enter your name</title> </head> <body> Enter your name and we will generate a 4 digit random number to be stored in our database. <br /><br /> <form action="uniquerandom.php" method="post"> Name <input type="text" name="user" size="30"> <br /> <input type="submit" value="Submit your name"> </form> </body> </html> And for the PHP script named: uniquerandom.php <?php //Step 1: PHP will connect to the database //Using the information we set above. //In this illustration, it uses an XAMPP local host //However this can be applied to any types of Apache based servers running PHP. $username = "root"; $password = "xxxxxxx"; $hostname = "localhost"; $database = "randomnumber"; $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); $selected = mysql_select_db($database,$dbhandle) or die("Could not select $database"); //Step 2: PHP will generate a random number from 1 to 10. //We will incorporate the previous script but we will change the minimum and maximum. $randomnumber =mt_rand(1,10); //Step 3: Query the database if the generated random exist or not, it it exist generate another random number the compare again. //If the while loop will become false (means that the generated number is now unique), it will exit the loop and proceed to //next statements while( $fetch = mysql_fetch_array( mysql_query("SELECT `random` FROM `randgenerated` WHERE `random`=$randomnumber") ) ) { $randomnumber =mt_rand(1,10); } //Step 4: We will extract the name on the form, but first we have to check if the user did not provide a blank or empty data. $data=trim($_POST['user']); if (strlen($data)==0) { die('You forget to enter your name, please press the back button of your browser'); } else { $name = $data; } //Step 5: Sanitize data before storing it in database. $name = mysql_real_escape_string($name); $randomnumber = mysql_real_escape_string($randomnumber); //Step 6: Insert both data into the database. mysql_query("INSERT INTO `randgenerated` (`name`,`random`) VALUES('$name','$randomnumber')") or die(mysql_error()); //Storing of data is complete. Give feedback to the user. echo 'Storing of data is complete.'; echo '<br />'; echo '<a href="http://localhost/form.htm">Click here to input data again.</a>'; //Step 7: Close the database connections mysql_close($dbhandle); ?> Implementing in Complex Applications The above code snippets are just examples. To incorporate them into complex applications, always check the actual data stored in MySQL to make sure it is storing unique values. It is much better to use a very narrow range (like the example above, using 1 to 10 as minimum and maximum values) to test your script to see if it is correct.
blog comments powered by Disqus |
|
|
|
|
|
|
|