How to Create New User Accounts with PHP

Adding Users to an Application Using PHP Code and MySQL Tables

© Mark Alexander Bain

Nov 14, 2009
How to Create New User Accounts with PHP, Mark Alexander Bain
An Internet application administrator can obtain information from new users and then create account for them, or they can use PHP code to do it all automatically

One very good reason for using PHP is the way it enables a web site programmer to automate server tasks. Take, for example, the task of creating a new user account. Instead of a system administrator obtaining a new user's details and then entering them onto a MySQL database, PHP will do all of that. Automatically. And it can even check to see if the new user name has already been used or not.

The PHP programmer does all of this by writing code that will:

  • work out whether the web page needs to display a blank form or if it should process the new user's details
  • if no user details have been entered then display a form asking for all relevant details (such as a proposed user name)
  • if user details have been entered then query the database to see if the user name already exists
  • inform the user if the id has already be taken
  • create the new id if it is available

And, this can all be achieved with just a few lines of PHP code.

Identifying Whether or Not the User Has Entered Their Details

The web page should display:

  • either the results of processing the user input
  • or a blank form requesting user details

The decision as to which one to do can be made quite simply:

<?php
$username = $_REQUEST["username"];
if (!isset($username)) {
echo "<form method=post>
Enter New Username<input name=username><br>
<input type=submit></form>";
} else {

Here code will be executed only if the user has entered details into a text box and then pressed the submit button.

Processing the User Inputs

If the user input is to be processed then the code's first job is to connect to the correct MySQL database, for example:

$mysql_host = "localhost";
$mysql_database = "my_web_site";
$mysql_user = "my_web_site_user";
$mysql_password = "dt56o96";
mysql_connect($mysql_host, $mysql_user, $mysql_password);
mysql_select_db($mysql_database);

This can be added into the PHP file itself, but it is much more efficient to save that to PHP file of its own, and then to use the include statement to access the code.

Does the User Already Exist?

The purpose of this particular PHP script is to create a new user account. It's first job, therefore, should be to ensure that this proposed account does not already exist. This can be done by formulating an appropriate SQL statement (and assumes that a table "USERS" already exist on the MySQL database):

$SQL = "select * from USERS where username='$username'";

And then running this query on the database:

$result = mysql_query($SQL) or die (mysql_error());

The programmer can now use the PHP mysql_numrows function to obtain the number of rows returned in the recordset:

$num = mysql_numrows($result);

If the number returned is not zero then the user name already exists.

Creating a new User Name

If any rows are returned from the above query then the user should be notified that their chosen user name has already been used:

if ($num > 0) {
echo "Username already exists<br>";

However, if the user name is available then a simple insert statement can be used to create it:

} else {
$SQL = "insert into USERS (username) values ('$username')";
mysql_query($SQL) or die (mysql_error());
}

And then then connection to the database can be closed:

mysql_close();
}
?>

If the programmer saves all of this into a PHP web page (for example "add_user.php") then they will have produced an easy and effective way of users being able to create their own accounts on any Internet based application.


The copyright of the article How to Create New User Accounts with PHP in PHP Programming is owned by Mark Alexander Bain. Permission to republish How to Create New User Accounts with PHP in print or online must be granted by the author in writing.


How to Create New User Accounts with PHP, Mark Alexander Bain
       


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo