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

Basic Form Code - 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.

  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



Now that the database has been set, we will start to write our form source code and PHP scripts. Let us start with our basic form. We will name this form.htm.



<title>Enter your name</title>



Enter your name and we will assign a random number from 1 to 10 to be stored in our database.

<br /><br />

<form action="generaterandom.php" method="post">

Name <input type="text" name="user" size="30">

<br />

<input type="submit" value="Submit your name">




After a user presses “Submit your name,” a PHP script named “generaterandom.php” will generate a random number from 1 to 10 and store that number along with the name in the MySQL database.

For the PHP script of generaterandom.php, the following is the flow of information:


//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 = "xxxxx";

$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: 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.


if (strlen($data)==0)


die('You forget to enter your name, please press the back button of your browser');




$name = $data;



//Step 4: Sanitize data before storing it in database.

$name = mysql_real_escape_string($name);

$randomnumber = mysql_real_escape_string($randomnumber);

//Step 5: 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 6: Close the database connections



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: