User Registration

First we'll need to create the form for users to enter their information. This will just be regular old HTML:

<html><head>
<title>User Registration</title>
</head><body>

<form action="register.php" method="post">
Pick a Username: <input type="text" name="username" size="20"><br>
Pick a Password: <input type="password" name="password" size="20"><br>
<input type="submit" value="Sign Up">
</form>

</body></html>

You can, of course, edit the HTML code to your liking, as long as the form remains intact, with it's username and password fields. If you added extra fields to your database table, you can fill them by adding the same fields to this form:

<form action="register.php" method="post">
Pick a Username: <input type="text" name="username" size="20"><br>
Pick a Password: <input type="password" name="password" size="20"><br>
Email Address: <input type="text" name="email" size="20"><br>

Real Name: <input type="text" name="realname" size="20"><br>
Location: <input type="text" name="location" size="20"><br>
<input type="submit" value="Sign Up">
</form>

Now we need to create register.php, which is the script the form above submits to. This script will submit the user's information to the MySQL database we set up.

<?php 

include("config.php"); 

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

// check if the username is taken
$check = "select id from $table where username = '".$_POST['username']."';"; 
$qry = mysql_query($check) or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry); 
if ($num_rows != 0) { 
echo "Sorry, there the username $username is already taken.
"; echo "Try again"; exit; } else { // insert the data $insert = mysql_query("insert into $table values ('NULL', '".$_POST['username']."', '".$_POST['password']."')") or die("Could not insert data because ".mysql_error()); // print a success message echo "Your user account has been created!
"; echo "Now you can log in"; } ?>

Here we include the connection info in config.php, and use it to connect to the MySQL server and select the database to use, the same we we did in create.php. We then search the table we created earlier for the entered username to see if it is already there. If it is, an error message is printed, otherwise we insert the username and password into the table. The data must be entered in the correct order; we created the table in the order of id, username, and password, so we enter the values in the same order.

Notice that for the first field, id, we are entering NULL. This is because id is auto_increment, so it will automatically be assigned an integer value. The first one will be 1, the second 2, and so on.

Again, if you created more fields in your table and signup form, add them to the $insert query, like this:

$insert = mysql_query("insert into $table values ('NULL', '".$_POST['username']."', '".$_POST['password']."', '".$_POST['email']."', '".$_POST['realname']."', '".$_POST['location']."')")

Make sure you put the variables in the same order as the fields on the table, or the information will end up in the wrong fields. After inserting the data (or registering the user!), the script then prints a success message with a link to the log-in page.

What log-in page, you ask? Let's write it now!