Published on: 2005-08-25 - Views: 25572

Digg! del.icio.us Furl reddit spurl bloglines ma.gnolia.com Yahoo MyWeb technorati blogmarks blinklist pixelgroovy Share this tutorial on tutorialicio.us! simpy
In this tutorial we'll see how to insert some values into a database after performing all the needed checks and security changes.

You can find the previous part of this tutorial HERE (to check the correctness of the values and eventually redirect users to custom error pages) and the other previous part HERE (to create the form we use for this whole tutorial).

Now we'll create the database table that will keep our users' data: open your phpmyadmin and run the following script into the SQL window:

DROP TABLE IF EXISTS `users`; //IMPORTANT: this delete any existing table called "users", pay attention if you have important datas in your database
CREATE TABLE `users` // this obviously creates the table named "users"
(
`id` int(11) NOT NULL auto_increment, //here we create all thecolumns that compose the table structure
`fullname` varchar(32) NOT NULL, //NOT NULL means that this field cant be empty
`username` varchar(32) NOT NULL UNIQUE,//UNIQUE means that there can't be 2 records with the same value in this field
`password` varchar(32) NOT NULL,
`email` varchar(32) NOT NULL UNIQUE,
`domain` varchar(32) default NULL, // DEFAULT NULL means that if nothing different is specified this field is set to NULL
`zip` char(5) NOT NULL,
`city` varchar(32) NOT NULL,
`state` varchar(32) NOT NULL,
`country` varchar(32) NOT NULL,
`address` varchar(32) NOT NULL,
`phone` char(12) default NULL,
PRIMARY KEY (`id`)
)
ENGINE=INNODB;
Now let's have a look at the PHP code:
<?php
//NOTE: if you come from the previous part of this tutorial you don't need
//to add these variable declarations.
//Here we assign some values to some variables, using the values sent by a
//form you must have previously created $user = $_POST[\'username\'];//get username from form
$pass = $_POST[\'password\'];//get password from form
$pass2 = $_POST[\'password2\'];//get password2 from form
$name = $_POST[\'name\'];//get name from form
$domain = $_POST[\'domain\'];//get domain from form
$zip = $_POST[\'zip\'];//get zip from form
$city = $_POST[\'city\'];//get city from form
$email = $_POST[\'email\'];//get email from form
$state = $_POST[\'state\'];//get state from form
$country = $_POST[\'country\'];//get country from form
$address = $_POST[\'address\'];//get address from form
$phone = $_POST[\'phone\'];//get phone from form
//****************//
//Here we connect to the database, you have to use your own data.
//If you need a tutorial that explains you how to connect to a database just browse our php tutorials $connection = mysql_connect("mysqlhost", "databasename", "dbusername");
//now we choose the database to be used
@mysql_select_db(databasename) or die( "Unable to select database");
//here we declare our sql query statement to see if the user already exists
$check = mysql_query("select username from users where username=\"$user\"");
$returned = mysql_fetch_array($check);
//if a user with the same username is returned we redirect the users to a
//previously created error page
if(!empty($returned))
{
header("Location: error-userexists.php"); //the user will be sent to this page
mysql_close($connection); // and we close the connection to the database Die();
}
else
{
//here we declare our sql query statement to see if the email address is
//already associated to another account $check = mysql_query("select email from users where email=\"$email\"");
$returned = mysql_fetch_array($check);
//if a user with the same email address is returned we redirect the users
//to a previously created error page, this check is performed to be sure
//that only one account per email is created
if(!empty($returned))
{
header("Location: error-emailexists.php"); //the user will be sent to this page
mysql_close($link); //and we close the connection to the database
Die();
}
else //if these checks go smooth
$pass=md5($pass); // we encrypt the passwotd with md5 function
//we declare our sql statement to insert the collected values into our database
$request = "INSERT INTO users values(NULL,\'$name\',\'$user\',\'$pass\', \'$email\',\'$domain\',\'$zip\',\'$city\',\'$state\',\'$country\', \'$address\',\'$phone\')";
$results = mysql_query($request);
if($results) //if this operation goes smooth we send the visitor to a
//previously created error page
{
header("Location: accountok.php"); //the user will be sent to this page
}
else //is any kind of issue is found {
header("Location: error-account.php");
//the user will be sent to
//this previously created page
}
mysql_close($link); // we close the connection
Die();
}
//****************// ?>