PHP and MySQL Register Form

PHP and MySQL Register Form

Introduction:
This tutorial will cover creating a register form with PHP/MySQL/HTMl.

Steps of Creation:
Step 1:
First we need to create a database to hold the user accounts. This is done through MySQL/PHPMyAdmin.

On your localhost/web host cPanel/Control Panel go to MySQL Databases and create a new one (for example; the name of your site - Mine will be "tutorial"). Next go to PHPMyAdmin and select the created database.

Once you are on the database in PHPMyAdmin, create a table to hold the data - in this case it's our users accounts. Let's call it "users".

We want four columns;
[Name - Type - Length - Additionals...]

id - int - 5 - Auto Increment (A_I/AI) - Primary Key
username - varchar - 250
password - varchar - 250
email - varchar - 250

Click create and now we are ready to insert the data.

Step 2:
Now we can create a register form in HTML for the users...

  1. <form action='register.php' method='POST'>
  2. <input type='text' name='user' />
  3. <input type='password' name='pass' />
  4. <input type='text' name='email' />
  5. <input type='submit' name='sentForm' />
  6. </form>

As you can see, the method is POST which means the data is sent from the form invisible to the average user and the action is 'register.php' (which I have saved the page as, this avoids having to create two pages for one register function).

Step 3:
Since the action on the form is the same page, I will insert the following function in to the same page. If you have set a different page, insert the below source code in to that page instead.

  1. <?php
  2. mysql_connect('localhost', 'root', '');
  3. mysql_select_db('tutorial');
  4. if (isSet($_POST['sentForm'])) {
  5. if (isSet($_POST['user']) && isSet($_POST['email']) && isSet($_POST['pass'])) {
  6. $user = $_POST['user'];
  7. $pass = $_POST['pass'];
  8. $email = $_POST['email'];
  9. $query = mysql_query("INSERT INTO `users` VALUES ('', $user, $pass, $email)");
  10. if ($q) {
  11. echo 'Successful!';
  12. }
  13. }
  14. }
  15. ?>

The above script connects to the database with the given information (server, username, password) and selects our database containing the users table. Then we get the information from the form through our POST data and insert them in to the users table in our database as a new row.

Testing
Once you have finished, it should all be working. Try to create an account and check the database to see if the account/row exists

BELOW IS THE COMPLETE CODE

<?php
	mysql_connect('localhost', 'root', '');
	mysql_select_db('tutorial');
	if (isSet($_POST['sentForm'])) {
		if (isSet($_POST['user']) && isSet($_POST['email']) && isSet($_POST['pass'])) {
			$user = $_POST['user'];
			$pass = $_POST['pass'];
			$email = $_POST['email'];
			$query = mysql_query("INSERT INTO `users` VALUES ('', $user, $pass, $email)");
			if ($q) {
				echo 'Successful!';
			}
		}
	}
?>
<form action='register.php' method='POST'>
	<input type='text' name='user' />
	<input type='password' name='pass' />
	<input type='text' name='email' />
	<input type='submit' name='sentForm' />
</form>

Please note:
This form does not have any security in place, I will cover the security in another tutorial and place the link to that tutorial here once it has been uploaded.