Home arrow PHP arrow Page 4 - Generating Contract Numbers for Online Licenses using PHP and MySQL

Generate Unique Contract numbers - PHP

PHP and MySQL form a powerful combination of open source technology available to any web developer. One of the increasing trends in modern web development is the generation of online contracts. E-commerce websites selling services online need automated contract generation for faster delivery of services. This article will help you learn to use this technology so you can get it working on your web site.

TABLE OF CONTENTS:
  1. Generating Contract Numbers for Online Licenses using PHP and MySQL
  2. Inserting generated random numbers into a MySQL database
  3. Basic Form Code
  4. Generate Unique Contract numbers
By: Codex-M
Rating: starstarstarstarstar / 8
April 28, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: