First off lets define the database table where our users will be stored. Using phpMyAdmin run this statement to create our table:
CODE
CREATE TABLE tblUsers (
fldId INT NOT NULL AUTO_INCREMENT,
fldUsername VARCHAR(40) NOT NULL,
fldPassword VARCHAR(40) NOT NULL
);
Now a little explanation as to what this will do. It will create a table in your database called tblUsers with fields fldId, fldUsername, and fldPassword. The last two fields are self explanitory they contain the username and password of the user. The fldId is the user id automatically assigned by the database. For more information on the syntax read the MySQL documentation.
Lets continue by creating the script where our users will register. Open your favorite text editor and enter the following:
CODE
<?php
?>
This tells the webserver that we are starting a php code section. You can have more than one in a script and you can include HTML in your code files as well, more on that later. Lets create a function that will actually do the work of adding the user to the database. Lets call it registerUser, now enter the following in between the php tags:
CODE
function registerUser() {
mysql_connect('server', 'username', 'password', 'database');
$username = $_POST['username'];
$password = md5($_POST['password']);
$sql = "INSERT INTO tblUsers (fldUsername, fldPassword) VALUES ($username, $password);";
mysql_query($sql);
}
We now have a very basic registration function. Now we need to create the form the user will see. So below the ?> lets start our HTML. It should look a bit like this:
CODE
<html>
<head>
<title>Registration</title>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF']."?register=true" ?>" method="post">
Username: <input type="text" name="username">
Password: <input type="password" name="password">
<input type="submit" value="Register">
</form>
</body>
</html>
Now this HTML defines a form with 2 input fields and a button. The thing to look at though is the action attribute of the form tag. Here we have another php code section. This puts the path of the current script as our action with the variable register equal to true. We will deal with that in our code later. For now your code should look like this:
CODE
<?php
function registerUser() {
mysql_connect('server', 'username', 'password', 'database');
$username = $_POST['username'];
$password = md5($_POST['password']);
$sql = "INSERT INTO tblUsers (fldUsername, fldPassword) VALUES ($username, $password);";
mysql_query($sql);
}
?>
<html>
<head>
<title>Registration</title>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF']."?register=true" ?>" method="post">
Username: <input type="text" name="username">
Password: <input type="password" name="password">
<input type="submit" value="Register">
</form>
</body>
</html>
There is one more thing left to do. Handle the variable we passed to the script called register. Lets do that now. Here is the code:
CODE
<?php
if ($_GET['register'] == 'true') {
registerUser();
}
function registerUser() { ....
Here we use an if statement to check and see if it has been set to true if it is we call the function we defined earlier.
That is all I will be doing for today. Later we will go over how to login, protect your pages and some basic error checking.


