Saturday 30 November 2013

Filled Under:

PHP registration form validation code with MySQL database connectivity

registration form in php and mysql with validation

Here i am providing  you the single php code required to create a simple signup/registration form with all the necessary validations and connectivity with mysql database so that the user input data can be stored in the database.

Software used : WAMP/XAMPP OR ANY SUPPORTING PHP.

The form will look like this :




Just follow these steps at time of using the code:

  • In the database connectivity portion of the code change the phpmyadmin username and password to that of yours.
  • Create the database "user_signup" and table "user" in phpmyadmin (code is provided for that as comment ).
  • In the "user_signup" database, Just run the  "create table..." query provided in run sql query section in phpmyadmin to create the table "user" .
  • Now save the code as "form1.php"  in the wamp/xampp root directory from where you will run the code in the browser.
  • when user fills up the form and press submit ,the data will be inserted in the database and user redirects to the same signup form.( change the header location in the code if you wish to direct user to some other page).

 The code is as follows :

 <html>  
 <head>  
 <?php  
 //form validation  
 // define variables and set to empty values  
 $fname = $lname = $email = $address = $gender = @$memtype = "";  
 @$uname = @$passn = "";  
 if ($_SERVER["REQUEST_METHOD"] == "POST")  
 {  
  $fname = test_input($_POST["fname"]);  
  $lname = test_input($_POST["lname"]);  
  $email = test_input($_POST["email"]);  
  @$address = test_input($_POST["add"]);  
  @$gender = test_input($_POST["gen"]);  
  @$uname = test_input($_POST["username"]);  
  @$passn = test_input($_POST["pass"]);  
  @$memtype = test_input($_POST["mtype"]);  
 }  
 function test_input($data)  
 {  
  $data = trim($data);  
  $data = stripslashes($data);  
  $data = htmlspecialchars($data);  
  return $data;  
 }  
 ?>  
 <?php  
 // define variables and initialize with empty values  
 $fnameErr = $lnameErr = $emailErr = $addressErr= $ageErr= $unameErr = $passnErr= $genderErr = $memtypeErr = "";  
 $fname1 = $age1 = $lname1 = $email1 = $address1 = $gender1 = $passn1 = $uname1 = $memtype1 = "";  
 if ($_SERVER["REQUEST_METHOD"] == "POST") {  
  $valid = true;  
      if(empty($_POST["fname"])) {  
     $fnameErr = "Missing Firstname";  
     $valid =false;  
      }  
   else {  
     $fname1 = test_input($_POST["fname"]);  
           // check if name only contains letters and whitespace  
   if (!preg_match("/^[a-zA-Z ]*$/",$fname))  
    {  
    $fnameErr = "Only letters and white space allowed";  
    $valid=false;  
       }  
   }  
      if (empty($_POST["lname"])) {  
     $lnameErr = "Missing Lastname";  
      $valid=false;  
      }  
   else {  
     $lname1 = test_input($_POST["lname"]);  
     // check if name only contains letters and whitespace  
   if (!preg_match("/^[a-zA-Z ]*$/",$lname))  
    {  
    $lnameErr = "Only letters and white space allowed";  
    $valid=false;  
       }  
      }  
      if (empty($_POST["email"])) {  
     $emailErr = "Missing email address";  
     $valid=false;  
      }  
   else {  
     $email1 = test_input($_POST["email"]);  
           // check if e-mail address syntax is valid  
   if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))  
    {  
    $emailErr = "Invalid email format";  
    $valid=false;  
       }  
   }  
   if (empty($_POST["add"])) {  
     $addressErr = "specify Address ";  
     $valid=false;  
      }  
   else {  
     $address1 = test_input($_POST["add"]);  
   }  
   if (!isset($_POST["gen"])) {  
     $genderErr = "specify your gender";  
     $valid=false;  
      }  
   else {  
     $gender1 = test_input($_POST["gen"]);  
   }  
      if (!isset($_POST["mtype"])) {  
     $memtypeErr = "specify your membership type";  
     $valid=false;  
      }  
   else {  
     $memtype1 = test_input($_POST["mtype"]);  
   }  
      if (empty($_POST["age"])) {  
   }  
   else {  
     $age = test_input($_POST["age"]);  
      if (preg_match("/^[a-zA-Z ]*$/",$age))  
    {  
    $ageErr = "Invalid age";  
    $valid=false;  
       }  
       }  
      if (empty($_POST["username"])) {  
     $unameErr = "specify your username";  
     $valid=false;  
      }  
   else {  
     $uname1 = test_input($_POST["username"]);  
   }  
      if (empty($_POST["pass"])) {  
     $passnErr = "specify your password";  
     $valid=false;  
      }  
   else {  
     $passn1 = test_input($_POST["pass"]);  
   }  
      }  
 // here form validation ends        
 ?>  
 <?Php  
 /*  
 --  
 -- Database: `user_signup`  
 --  
 -- --------------------------------------------------------  
 --  
 -- Table structure for table `user`  
 --  
 CREATE TABLE IF NOT EXISTS `user` (  
  `FirstName` char(30) NOT NULL,  
  `LastName` char(30) NOT NULL,  
  `Email` char(30) DEFAULT NULL,  
  `CPhone` mediumtext,  
  `Address` varchar(70) DEFAULT NULL,  
  `Age` int(11) DEFAULT NULL,  
  `Gender` char(10) DEFAULT NULL,  
  `UserName` varchar(20) NOT NULL,  
  `Password` varchar(20) NOT NULL,  
  `MType` char(10) NOT NULL,  
  `UId` int(11) NOT NULL AUTO_INCREMENT,  
  PRIMARY KEY (`UId`)  
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;  
 */  
 ?>  
 <?php  
 //mysql database connectivity  
 //inserting form data into the database  
 if(@$valid==true)  
       {  
 $con= mysql_connect("localhost","username","password");  //change the username and password  
 mysql_select_db("user_signup", $con);  
 @$a=$_POST['fname'];  
 @$b=$_POST['lname'];  
 @$c=$_POST['email'];  
 @$d=$_POST['cell'];  
 @$e=$_POST['add'];  
 @$f=$_POST['age'];  
 @$g=$_POST['gen'];  
 @$i=$_POST['username'];  
 @$j=$_POST['pass'];  
 @$h=$_POST['mtype'];  
 mysql_query("insert into user (FirstName,LastName,Email,CPhone,Address,Age,Gender,Username,Password,MType) values('$a','$b','$c','$d','$e','$f','$g','$i','$j','$h')");  
 header("Location:form1.php");  
 mysql_close($con);  
       }  
 ?>  
 </head>  
 <style>  
 div.ex  
 {  
 width:600px;  
 padding:30px;  
 border:7px solid red;  
 margin:4px;  
 }  
 body  
 {  
 background-image:url("bg.jpg");  
 }  
 </style>  
 <body >  
 <center>  
 <h2><b><i><u>Signup</u></i></b></h2>  
 <div class="ex">  
 <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" >  
 <table id="rtid1" border="0" cellpadding="5" cellspacing="0" width="600">  
 <tr id="rtrid1">  
 <td><b>FirstName*:</b></td>  
 <td>  
 <input id="FirstName" name="fname" value="<?php echo htmlspecialchars($fname);?>" type="text" maxlength="60" style="width:146px; border:1px solid #999999" />  
 <span class="error"><font color="red"><i><?php echo "&nbsp&nbsp&nbsp".$fnameErr;?></i></font></span>  
 </td>  
 </tr>   
 <tr>  
 <td><b>lastName*:</b></td>  
 <td>  
 <input id="LastName" name="lname" value="<?php echo htmlspecialchars($lname);?>" type="text" maxlength="60" style="width:146px; border:1px solid #999999" />  
 <span class="error"><font color="red"><i><?php echo "&nbsp&nbsp&nbsp".$lnameErr;?></i></font></span>  
 </td>  
 </tr>  
 <tr>  
 <td>  
 <b>Email address*:</b>  
 </td>  
 <td>  
 <input id="FromEmailAddress" name="email" type="text" value="<?php echo htmlspecialchars($email);?>" maxlength="60" style="width:200px; border:1px solid #999999" />  
 <span class="error"><font color="red"><i><?php echo "&nbsp&nbsp&nbsp".$emailErr;?></i></font></span>  
 </td>  
 </tr>  
 <tr id="rtrid2">  
 <td><b>Cell Phone:</b></td>  
 <td><input id="CellPhone" name="cell" type="text" maxlength="43" style="width:200px; border:1px solid #999999" /></td>  
 </tr>  
 <tr id="rtrid3">  
 <td><b>Address *:</b></td>  
 <td><input id="StreetAddress1" name="add" type="textarea" value="<?php echo htmlspecialchars($address);?>" maxlength="120" style="width:300px; border:1px solid #999999" />  
 <span class="error"><font color="red"><i><?php echo "&nbsp&nbsp&nbsp".$addressErr;?></i></font></span>  
 </td>  
 </tr>  
 <tr id="rtrid4">  
 <td><b>Age:</b></td>  
 <td>  
 <input id="Age" name="age" type="text" maxlength="120" style="width:300px; border:1px solid #999999" />  
 <span class="error"><font color="red"><i><?php echo "&nbsp&nbsp&nbsp".$ageErr;?></i></font></span>  
 </td>  
 </tr>  
 <tr id="rtrid5">  
 <td><b>Gender*:</b></td>  
 <td>  
 <input type="radio" name="gen" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male" id= "rgm">Male  
 <input type="radio" name="gen" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female" id="rgf">Female  
 <span class="error"><font color="red"><i><?php echo "&nbsp&nbsp&nbsp".$genderErr;?></i></font></span>  
 </td>  
 </tr>  
 <tr >  
 <td><b>UserName*:</b></td>  
 <td>  
 <input id="Username" name="username" type="text" value="<?php echo htmlspecialchars($uname);?>" maxlength="60" style="width:146px; border:1px solid #999999" />  
 <span class="error"><font color="red"><i><?php echo "&nbsp&nbsp&nbsp".$unameErr;?></i></font></span>  
 </td>  
 </tr>  
 <tr>  
 <td><b>Password*:</b></td>  
 <td>  
 <input id="Password" name="pass" type="password" value="<?php echo htmlspecialchars($passn);?>" maxlength="60" style="width:146px; border:1px solid #999999" />  
 <span class="error"><font color="red"><i><?php echo "&nbsp&nbsp&nbsp".$passnErr;?></i></font></span>  
 </td><br>  
 </tr>  
 <tr id="rtrid6">  
 <td><b>Membership type*:</b></td>  
 <td>  
 <input type="radio" name="mtype" <?php if (isset($memtype) && $memtype=="ordinary") echo "checked";?> value="ordinary">Ordinary     
 <input type="radio" name="mtype" <?php if (isset($memtype) && $memtype=="premium") echo "checked";?> value="premium">Premium  
 <span class="error"><font color="red"><i><?php echo "&nbsp&nbsp&nbsp".$memtypeErr;?></i></font></span>  
 </td>  
 </tr>  
 <td colspan="2" align="center">  
 <br />  
 <br><input id="skip_Submit" name="skip_Submit" type="submit" value="Submit" /></br>  
 </br>  
 </td>  
 </tr>  
 </table>  
 <br/>  
 </form>  
 </div>  
 </center>  
 </body>  
 </html>  









1 comments:

  1. How to create database table in mysqli in php using C panel

    ReplyDelete

Turingo: Answer Your Curiosity

Create questions | Search questions | Get your answers | Join Communities | Create your teams | Get answers to your curiosity