Skip to main content

Designing a User-Registration Script in PHP

Designing a User-Registration Script in PHP

Having enough knowledge of Storing and Retrieving Data from MySQL Database we now can easily create a User Registration System to register new users for sites requiring user authorization. We’ve previously created many scripts and applications that require user authorization but none of them can register new users by themselves. We had to hard code the user data into the scripts. For any real application though, that won’t work and we require a “New User Registration’ system. That’s what we are going tot create in this post.

Registering new user is just a matter of storing username, password and optionally other information about the user in a database. As we have discussed Storing and Retrieving Data from MySQL Database, so we straightaway look at the code listing:

<html>
<head>
<title>My Web Site | User Registration</title>
</head>

<body>
<h1>My Web Site</h1>
<h2>User Registration</h2>
<?php 
if(isset($_POST['register']))
{
    
$name=$_POST['name'];
    
$uname=$_POST['uname'];
    
$pass1=$_POST['pass1'];
    
$pass2=$_POST['pass2'];
    
$email=$_POST['email'];
    
    
//chek if all the informations are entered or not
    
if($name!='' && $uname!='' && $pass1!='' && $pass2!='' && $email!='')
    {
//if properly filled
        //check if both passwords match
        
if($pass1==$pass2)
        {
            
//connect to MySQL 
            //provide your 'USERNAME' and 'PASSWORD' 
            //change 'localhost' to the MySQL server 
            //host, if not using on 'local server' 
            
$db=new mysqli('localhost','USERNAME','PASSWORD');

            
//if this is the first time
            //and database is not craeted
            
if(!$db->select_db('one'))
            
//create the database
            
$db->query('create database one');
        
            
//select the databasw to work with
            
$db->select_db('one');
            
            
//if table is not created, create it
            
if(!$db->query('select * from user'))
                
$db->query('create table user (userid int auto_increment primary key, name varchar(50), uname varchar(15), email varchar(50), pass varchar(15))');
        
            
//ready to insert data
            //check if username entered is available or not
            
$result=$db->query("select uname from user where uname='$uname'");
            if(
$result->num_rows>0)
                echo 
"<p style=\"color: #0000ff\"><i>$uname</i> already exists. Please choose a different UserName.</p>";
            else
            {
                
$db->query("insert into user (name, uname, email, pass) values ('$name', '$uname', '$email', '$pass1')");
                echo 
"<p style=\"color: #0000ff\">Registered Sucessfully!</p>";
                
//don't show form on successful completion
                
exit;
            }
        }
        
//if passwords don't match
        
else echo "<p style=\"color: #ff0000\">Both passwords do not match! Please re-enter and register.</p>";
    }
    
//if not poperly filled
    
else echo "<p style=\"color: #ff0000\">You haven't filled all the information. Please Re-Enter.</p>";
}

//close MySQL connection
$db->close();

//show the form
?>
<code><span style="color: #000000"><span style="color: #007700"> </span></span></code> 
<form name="form1" id="form1" method="post" action="">
  <table width="500" border="0" cellspacing="0" cellpadding="10">
    <tr> 
      <td width="133">Name</td>
      <td width="327"><input name="name" type="text" id="name" value=""></td>
    </tr>
    <tr> 
      <td>UserName</td>
      <td><input name="uname" type="text" id="uname" value=""></td>
    </tr>
    <tr> 
      <td>E-Mail Address</td>
      <td><input name="email" type="text" id="email" /></td>
    </tr>
    <tr> 
      <td>Password</td>
      <td><input name="pass1" type="password" id="pass1" /></td>
    </tr>
    <tr> 
      <td>Re-Type Password</td>
      <td><input name="pass2" type="password" id="pass2" /></td>
    </tr>
    <tr> 
      <td><input name="register" type="submit" id="register" value="Register" /></td>
      <td><div align="right"> 
          <input type="reset" name="Submit2" value="Reset" />
        </div></td>
    </tr>
  </table>
</form>
</body>
</html>

We’d easily be able to integrate this to our existing ‘user authorization‘ sites that we had created. We would just have to query the database to match username and password rather than with variables or constants.

These are the lines in the ‘login’ page that match username and password:

//user data
$user='one';
$pass='123';

...
...
...
//match username & password
if(
$username==$user && $password==$pass)
{

which can be changed as following:

$result=$db->query("select pass from user where uname='$username'");
if(
$result->num_rows>0
{
    
$pass=$result->fetch_row();
    
$pass=$pass[0];
    if(
$password=$pass)
    {

Previous Articles:

Popular posts from this blog

Fix For Toshiba Satellite "RTC Battery is Low" Error (with Pictures)

RTC Battery is Low Error on a Toshiba Satellite laptop "RTC Battery is Low..." An error message flashing while you try to boot your laptop is enough to panic many people. But worry not! "RTC Battery" stands for Real-Time Clock battery which almost all laptops and PCs have on their motherboard to power the clock and sometimes to also keep the CMOS settings from getting erased while the system is switched off.  It is not uncommon for these batteries to last for years before requiring a replacement as the clock consumes very less power. And contrary to what some people tell you - they are not rechargeable or getting charged while your computer or laptop is running. In this article, we'll learn everything about RTC batteries and how to fix the error on your Toshiba Satellite laptop. What is an RTC Battery? RTC or CMOS batteries are small coin-shaped lithium batteries with a 3-volts output. Most laptops use

The Best Way(s) to Comment out PHP/HTML Code

PHP supports various styles of comments. Please check the following example: <?php // Single line comment code (); # Single line Comment code2 (); /* Multi Line comment code(); The code inside doesn't run */ // /* This doesn NOT start a multi-line comment block /* Multi line comment block The following line still ends the multi-line comment block //*/ The " # " comment style, though, is rarely used. Do note, in the example, that anything (even a multi-block comment /* ) after a " // " or " # " is a comment, and /* */ around any single-line comment overrides it. This information will come in handy when we learn about some neat tricks next. Comment out PHP Code Blocks Check the following code <?php //* Toggle line if ( 1 ) {      // } else {      // } //*/ //* Toggle line if ( 2 ) {      // } else {      // } //*/ Now see how easy it is to toggle a part of PHP code by just removing or adding a single " / " from th

How to Create an HTML Form Linked with MySQL Database in PHP

If you're looking for example code and detailed discussion on how to create an HTML form that stores data in a MySQL database using PHP then this post might be what you're looking for. I assume that you're familiar with basic HTML, CSS, PHP coding, and  MySQL. I am going to divide this small project into two parts: The HTML form itself that takes input from the user and the PHP script that saves it into the database A table that displays the user-added data that has been saved in the database. We'll be dealing with the first part in this tutorial. Again I'd like to break this problem into a few parts so that it's easier for you to understand and probably gives you an insight into how breaking up a problem into smaller chunks can help make things clearer in your mind. Let's think about it, there is an HTML form (that is HTML code), then there is the PHP code that deals with the user-input data, and the MySQL database itself. For this tutorial, we'll b