PHP
  Home arrow PHP arrow Page 4 - Generating Contract Numbers for Online Licenses using PHP and MySQL
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Generating Contract Numbers for Online Licenses using PHP and MySQL
By: Codex-M
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2009-04-28


    Table of Contents:
  • Generating Contract Numbers for Online Licenses using PHP and MySQL
  • Inserting generated random numbers into a MySQL database
  • Basic Form Code
  • Generate Unique Contract numbers

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Generating Contract Numbers for Online Licenses using PHP and MySQL - Generate Unique Contract numbers
    ( Page 4 of 4 )

    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. 



     
     
    >>> More PHP Articles          >>> More By Codex-M
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek