
PHP Register Page
Tutorial by: Epatron
- Connect to MYSQL DB
- HTML Form
- Code
- PHP Part
- Create Table
- Register Code
- Simple
- Little Detailed
- Fully Detailed
- Simple
- Create Table
We need a file named connect.php which connects us to mysql database! We will include that to other file later at this tutorial!
Go HERE, to find out how the connection is made!
HTML Form:
Create file named index.html and do simple register form, please understand that I'm not going to style forms , you can do it, if you want!
<html> <head><title>My Register Page</title></head> <body><form action="register.php" method="post"> <h2>Register:</h2><br /><br /> <b>User Name:</b><br /> <input type="text" name="username" /><br /> <b>Password:</b><br /> <input type="password" name="password" /><br /> <b>Re-type Password:</b><br/> <input type="password" name="repassword" /><br /> <input type="submit" value="Register!" /></form> </body> </html>I presume that you understand html forms well, so I'm not going to explain it!
PHP Part:
Create Table:
We need to create table to our database, so we can save all registered users to database!
You can now choose how you wanna do the table to database! WITH PHP or SQL
With PHP:
Create a file with name: install.php and put this code into it:
<?php
include ("connect.php"); // Includes the connect.php ! You don't have? Check tutorials first part "Connect to mysql DB" again!
mysql_query("create table users(
username varchar(30) NOT NULL,
password varchar(32) NOT NULL,
PRIMARY KEY (username)
)") or die (mysql_error());
echo "Table created! DELETE THE INSTALL.PHP NOW!";
?>
If you get "Table created!" text, table is created to your database, and you need to delete the install.php from your folder!I will explain the code after showing the SQL way to do table!
With SQL:
Go to phpmyadmin and select SQL above! Image instructions:

and insert this code into the box:
CREATE TABLE users( username varchar(30) NOT NULL, password varchar(32) NOT NULL, PRIMARY KEY (username) )Explaining:
CREATE TABLE users(Kinda explains it by itself ! So this code creates table named 'users' don't forget the ( to the beginning!
username varchar(30) NOT NULL, password varchar(32) NOT NULL,"username" and "password" are the field names, "varchar" its the data type of the field and it will contain the letters, "(30)" and "(32)" are the maxium length of the fields and "NOT NULL" means that the field can't be empty.
PRIMARY KEY (username) )Primary key is an "unique identifier", means that there can't be two with the same text (username).
Register code:
Now It's time to code the hardest thing at the register!
We're gonna use functions: mysql_query and mysql_num_rows!
Create file named: register.php and go through all the points:
Simple Register code:
I don't recommend to use this, but you should go trough this code, because I will some parts at this O N L Y!
And you will learn the register code better!
<?php
include ("connect.php");
mysql_query("INSERT into users VALUES('".$_POST['username']."','".$_POST['password']."')") or die(mysql_error());
?>
This is a really simple register code, you can try and check is it working! But as you might noticed we need to check this:-Username already in use? and -Does the passwords matches?
But before adding them, I want to explain this code, because it will be on better codes too!
Explaining:
"mysql_query" is function which asks the MYSQL database to do something, in this case we're asking to "INSERT INTO users" table, and
after that line it tells what we will insert to users table! "VALUES" and we will insert the username and password from form!
Little detailed register code:
I don't recommend this either, only the Fully detailed is what I want to you use, but just for learning:
<?php
include ("connect.php");
//Let's do code for if username already is in use!
if(mysql_num_rows(mysql_query("SELECT * from users WHERE username='" . $_POST['username'] . "'")) == 1){
echo "Oops! Username is already in use";
}else{
mysql_query("INSERT into users VALUES ('".$_POST['username']."', '".$_POST['password']."')") or die(mysql_error());
}
?>
Now, we added if statement which checks that is username avaible or not, if its already in use you will get message: "Sorry..."We'are using SELECT * from to check the username exist.
And now you might think: Is it possible to add more errors like that username already taken? Answer is YES!
Fully detailed register page:
I recommend to use this code, it checks these things:
-Unknown letters in username, -Username already taken, -Username too long, -Username too short
-Unknown letters in password, -Two entered passwords wont match, -Password too long, -Password too short.
And if the user is created it will lead you back to index.html !
<?php
include ("connect.php");
if(mysql_num_rows(mysql_query("SELECT * from users WHERE username='" . $_POST['username'] . "'")) == 1){
echo "Oops! Username is already in use!";
}
else if($_POST['password'] != $_POST['retype-password']){
echo "Oops! The two entered passwords don`t match!";
}
else if(strlen($_POST['username']) > 15){
echo "Oops! Username is too long!";
}
else if(strlen($_POST['username']) < 6){
echo "Oops! Username is too short!";
}
else if(strlen($_POST['password']) > 15){
echo "Oops! Password is too long!";
}
else if(strlen($_POST['password']) < 6){
echo "Oops! Password is too short!";
}
else if(preg_match('/[^0-9A-Za-z]/',$_POST['username'])){
echo "Oops! Unknown letters at username!";
}
else if(preg_match('/[^0-9A-Za-z]/',$_POST['password'])){
echo "Oops! Unknown letters at password!";
}
else{
mysql_query("INSERT into users VALUES ('".$_POST['username']."', '".$_POST['password']."')") or die(mysql_error());
header('Location: index.html');
}
?>
Gz! You know have working register page! If you want to keep up the log of the user registers, check this tutorial about the php file functions!You will learn log-in at the next tutorial!
Errors / Questions? Send message below or PM me ! :)
Edited by Epatron, 14 August 2011 - 03:56 AM.


Sign In
Create Account


Back to top









