How to Create User Registration in PHP
Hi , in this post we will disscuss about how to create user registration in PHP and MySQL going to some easy steps. user registration form is most imoprtant thing for any kind of websites. user registration is one of the most common components of any membership driven websites and the ability to make them is a must for web developers . Lets start the creating user registration in PHP Follow the given below steps.
How to Create User Registration in PHP
1-Creating Database
- Open Phpmyadmin in your Browser
- Click on Database Tab Display on Top side
- Give the Database name “user_registration_1”.
- After Creating Database Open it.
- Click on SQL Tab on Top area
- Copy the Below Source Code and paste it.
- Then Click on Go.
-- phpMyAdmin SQL Dump -- version 4.2.7.1 -- http://www.phpmyadmin.net -- -- Host: 127.0.0.1 -- Generation Time: Apr 27, 2016 at 06:24 PM -- Server version: 5.6.20 -- PHP Version: 5.5.15 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- Database: `user_registration_1` -- -- -------------------------------------------------------- -- -- Table structure for table `registered_users` -- CREATE TABLE IF NOT EXISTS `registered_users` ( `id` int(8) NOT NULL, `user_name` varchar(255) NOT NULL, `first_name` varchar(255) NOT NULL, `last_name` varchar(255) NOT NULL, `password` varchar(25) NOT NULL, `email` varchar(55) NOT NULL, `gender` varchar(20) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ; -- -- Dumping data for table `registered_users` -- INSERT INTO `registered_users` (`id`, `user_name`, `first_name`, `last_name`, `password`, `email`, `gender`) VALUES (7, 'Deepak', 'Deepak', 'Raj', 'deepak', 'example@mail.com', 'Male'), (8, 'Lucky', 'Lucky', 'Kumar', 'lucky', 'example@mail.com', 'Male'), (9, 'Rani', 'Rani', 'Kumari', 'rani', 'example@mail.com', 'Female'), (10, 'Priyanshu', 'Priyanshu', 'Kumar', 'priyanshu', 'example@mail.com', 'Male'); -- -- Indexes for dumped tables -- -- -- Indexes for table `registered_users` -- ALTER TABLE `registered_users` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `registered_users` -- ALTER TABLE `registered_users` MODIFY `id` int(8) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=11; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
2- Import DB File
After Downloading the source code extract it in your root folder.
- Open Phpmyadmin in your Browser
- Click on Database Tab Display on Top side
- Give the Database name “user_registration_1”.
- After Creating Database Open it.
- Click on Import Tab on Top area
- You can Find Db file in Downloaded source code Select it.
- Then Click on Go.
3- Creating Database Connection
After import Database File then next step is creating database connection using php copy the below code and save it is as “db.php”.
<?php class DB { private $host = "localhost"; private $user = "root"; private $password = ""; private $database = "user"; function __construct() { $conn = $this->connectDB(); if(!empty($conn)) { $this->selectDB($conn); } } function connectDB() { $conn = mysql_connect($this->host,$this->user,$this->password); return $conn; } function selectDB($conn) { mysql_select_db($this->database,$conn); } function runQuery($query) { $result = mysql_query($query); while($row=mysql_fetch_assoc($result)) { $resultset[] = $row; } if(!empty($resultset)) return $resultset; } function numRows($query) { $result = mysql_query($query); $rowcount = mysql_num_rows($result); return $rowcount; } function updateQuery($query) { $result = mysql_query($query); if (!$result) { die('Invalid query: ' . mysql_error()); } else { return $result; } } function insertQuery($query) { $result = mysql_query($query); if (!$result) { die('Invalid query: ' . mysql_error()); } else { return $result; } } function deleteQuery($query) { $result = mysql_query($query); if (!$result) { die('Invalid query: ' . mysql_error()); } else { return $result; } } } ?>
3 – Creating the Registration form
in this step we will create a simple HTML form where user fill your persional information and submit . copy the below source code and use it.
<form method="post" action=""> <table border="0" width="500" align="center" class="demo-table"> <div class="alert_message"><?php if(isset($alert_message)) echo $alert_message; ?></div> <tr> <td class="text_label">Username</td> <td><input type="text" class="table_container" autofocus="autofocus" name="userName" value="<?php if(isset($_POST['userName'])) echo $_POST['userName']; ?>"></td> </tr> <tr> <td class="text_label">First Name</td> <td><input type="text" class="table_container" autofocus="autofocus" name="firstName" value="<?php if(isset($_POST['firstName'])) echo $_POST['firstName']; ?>"></td> </tr> <tr> <td class="text_label">Last Name</td> <td><input type="text" class="table_container" autofocus="autofocus" name="lastName" value="<?php if(isset($_POST['lastName'])) echo $_POST['lastName']; ?>"></td> </tr> <tr> <td class="text_label">Password</td> <td><input type="password" class="table_container" autofocus="autofocus" name="password" value=""></td> </tr> <tr> <td class="text_label">Confirm Password</td> <td><input type="password" class="table_container" autofocus="autofocus" name="confirm_password" value=""></td> </tr> <tr> <td class="text_label">Email</td> <td><input type="text" class="table_container" autofocus="autofocus" name="userEmail" value="<?php if(isset($_POST['userEmail'])) echo $_POST['userEmail']; ?>"></td> </tr> <tr> <td class="text_label">Gender</td> <td><input type="radio" name="gender" value="Male" <?php if(isset($_POST['gender']) && $_POST['gender']=="Male") { ?>checked<?php } ?>> Male <input type="radio" name="gender" value="Female" <?php if(isset($_POST['gender']) && $_POST['gender']=="Female") { ?>checked<?php } ?>> Female </td> </tr> </table> <div> <input type="submit" name="submit" value="Add User" class="btnRegister"> </div> </form>
4 – Styling the form using CSS
in this step give some style to form using css and make more attractive. follow these step given below.
<style type="text/css"> body { width:800px; margin:auto; } .alert_message { color: #FF0000; font-weight: bold; margin-left: 30px; margin-top: 10px; } .demo-table { border:red 2px solid; color:black; margin:20px; } .demo-table td { padding:5px; } .table_container { font-size:18px; border:red 1px solid; border-radius: 4px; text-indent:5px; } .btnRegister { background-color:azure; border:red 1px solid; color: red; width:100px; padding:10px; font-size:20px; margin-left:22px; border-radius:2px; cursor: pointer; } .btnRegister:hover { background:azure; border:green 1px solid; color: green; width:100px; padding:10px; font-size:20px; margin-left:22px; border-radius:10px; cursor: pointer; } .text_label { font-size:20px; font-weight:bold; color:red; font-family:Helvitica; text-align:center; } .table_data { width: 86%; margin:10px; } </style>
5 – Validating the form and Send Data into Database using PHP
next step is validating the form using php. in this step we validate the form into serverside it means when click on submit button all information fill in form send into database. Follow the Below Source Code.
<?php if(count($_POST)>0) { /* Form Required Field Validation */ foreach($_POST as $key=>$value) { if(empty($_POST[$key])) { $alert_message = ucwords($key) . " Field is required"; break; } } /* Password Matching Validation */ if($_POST['password'] != $_POST['confirm_password']){ $alert_message = 'Passwords does not much. Try again.<br>'; } /* Email Validation */ if(!isset($alert_message)) { if (!filter_var($_POST["userEmail"], FILTER_VALIDATE_EMAIL)) { $alert_message = "Invalid your email."; } } /* Validation to check if gender is selected */ if(!isset($alert_message)) { if(!isset($_POST["gender"])) { $alert_message = " Gender field is required"; } } if(!isset($alert_message)) { require_once("db.php"); $db_handle = new DB(); $query = "INSERT INTO registered_users (user_name, first_name, last_name, password, email, gender) VALUES ('" . $_POST["userName"] . "', '" . $_POST["firstName"] . "', '" . $_POST["lastName"] . "', '" . ($_POST["password"]) . "', '" . $_POST["userEmail"] . "', '" . $_POST["gender"] . "')"; $result = $db_handle->insertQuery($query); if(!empty($result)) { $alert_message = "User Successfully Registered!"; unset($_POST); } else { $alert_message = "Incorrect Details. Try Again!"; } } } ?>
6 – Display Data on Browser From Database
in this step we are goint to display all data from database on browser. follow the below source code use it..
<h1>All Registered User</h1> <table style="border:2px red solid;" class="table_data" cellspacing="5" cellpadding="5"> <tr style="color:black;"> <th style="border-bottom:4px blue solid; border-right:4px blue solid;" > User Name </th> <th style="border-bottom:4px green solid; border-right:4px green solid;"> First Name </th> <th style="border-bottom:4px gray solid; border-right:4px gray solid;"> Last Name </th> <th style="border-bottom:4px red solid; border-right:4px red solid;"> Password </th> <th style="border-bottom:4px black solid; border-right:4px black solid;"> Email </th> <th style="border-bottom:4px black solid; border-right:4px black solid;"> Gender </th> </tr> <?php $conn = mysql_connect("localhost","root",""); mysql_select_db("user",$conn); $result= mysql_query("select * from registered_users order by id DESC ") or die (mysql_error()); while ($row= mysql_fetch_array ($result) ){ $id=$row['id']; ?> <tr style="text-align:center; color:red;"> <td style="width:200px; border-bottom:4px blue solid; border-right:4px blue solid;"> <?php echo $row['user_name']; ?> </td> <td style="width:200px; color:red; border-bottom:4px green solid; border-right:4px green solid;"> <?php echo $row['first_name']; ?> </td> <td style="width:200px; color:red; border-bottom:4px gray solid; border-right:4px gray solid;"> <?php echo $row['last_name']; ?> </td> <td style="width:200px; color:red; border-bottom:4px red solid; border-right:4px red solid;"> <?php echo $row['password']; ?> </td> <td style="width:200px; color:red; border-bottom:4px black solid; border-right:4px black solid;"> <?php echo $row['email']; ?> </td> <td style="width:200px; color:red; border-bottom:4px black solid; border-right:4px black solid;"> <?php echo $row['gender']; ?> </td> </tr> <?php } ?> </table>
If you facing any type of problem with this source code then you can Download the Complete source code in zip Formate by clicking the below button Download Now otherwise you can send Comment.