PHP Registration Form

Created by Phil Thompson

For most people I would imagine that if you need a registration form you would be looking at PHP to process the data and put it into your database. This is one of the things that PHP is really good at doing especially when paired up with a mySQL database. In this demo I am starting with a simple html form that just has a little bit of css formatting to change the look and "tart" it up a bit.   HTML
 <!DOCTYPE HTML>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Registration</title>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all">
</head>

<body>
<div id="page-wrap">
<h1>Registration Form</h1>

<form method="post" action="./reg.php">
<div id="col-left">
First Name<br /><br />
Last Name<br /><br />
Username<br /><br />
Email Address<br /><br />
Password
</div>
<div id="col-right">
<input type="name" name="firstName" autocomplete="off" autofocus class="area" /><br>
<input type="name" name="lastName" autocomplete="off" class="area" /><br>
<input type="text" name="userName" autocomplete="off" class="area" /><br>
<input type="email" name="emailAddress" autocomplete="off" class="area" /><br>
<input type="password" name="passWord" autocomplete="off" class="area" /><br>
<input type="submit" />
</div>
<div class="clear"></div>
</form>
</div>
</body>
</html> 
  CSS
 * { margin: 0px; padding: 0px; }

body {
font: 10px Arial, Helvetica, sans-serif;
background: #CCC;
}

#page-wrap {
width: 600px;
margin: 10px auto 0;
background: #FFF;
padding: 10px;
-moz-box-shadow: 10px 10px 5px #888;
-webkit-box-shadow: 10px 10px 5px #888;
box-shadow: 10px 10px 5px #888;
}

#col-left {
width: 50%;
float: left;
font-size: 14px;
text-align: right;
}

#col-right {
width: 50%;
float: right;
}

.clear {
clear: both;
}

input {
margin: 0 0 11px 0;
}

input.area {
border-style: ridge;
border-color: #CCC;
} 
  Now you will see that the html form is sending it's data to "reg.php".     Now onto the PHP it self, the first thing we do is grab all the data that is sent to it.
 

<?php

$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$userName = $_POST['userName'];
$emailAddress = $_POST['emailAddress'];
$passWord = $_POST['passWord']; 
  After this it is advised to encrypt the users password for security. The encryption we will use is MD5 hash by using the inbuilt PHP function of md5().
 $passWord = md5($passWord); 
  The next thing we need to do now is make the connection to the  mySQL database and teach the script which table to use.
 

$host = 'location/of/database';
$dbuser = 'database/username';
$dbpass = 'database/password';
$dbname = 'name/of/database';

$con = mysql_connect($host,$dbuser,$dbpass);

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db ($dbname)

 
  Now the connection is made we can now upload the data to the mySQL database and use a IF statement to report back to the user if the registration works.
 

$sql = "INSERT INTO users (`firstName`, `lastName`, `emailAddress`, `userName`, `passWord`) VALUES ('$firstName', '$lastName', '$emailAddress', '$userName', '$passWord')";

if (mysql_query($sql)) { ?>

<h1> Welcome </h1>

<?php } else { ?>

<h1> Failed Try Again </h1><br />
<?php mysql_error(); ?>

<?php } 
  And finally we close the mySQL connection.
 

mysql_close($con);

?> 
  Now you are done here is the full script.
 

<?php

$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$userName = $_POST['userName'];
$emailAddress = $_POST['emailAddress'];
$passWord = $_POST['passWord'];

$passWord = md5($passWord);

$host = 'location/of/database';
$dbuser = 'database/username';
$dbpass = 'database/password';
$dbname = 'name/of/database';

$con = mysql_connect($host,$dbuser,$dbpass);

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db ($dbname)

$sql = "INSERT INTO users (`firstName`, `lastName`, `emailAddress`, `userName`, `passWord`) VALUES ('$firstName', '$lastName', '$emailAddress', '$userName', '$passWord')";

if (mysql_query($sql)) { ?>

<h1> Welcome </h1>

<?php } else { ?>

<h1> Failed Try Again </h1><br />
<?php mysql_error(); ?>

<?php }

mysql_close($con);

?> 
comments powered by Disqus