In previous post we have learned to create a secure login system with session. Now let’s have look at How to Create a Registration Form with PHP and MySQLi. Well it’s quite easy and can be use for different purposes. It can be use to Register a new member on your membership website or you can use it to get some inputs from the user for further processing, well it doesn’t matter for what you want a registration form the code is same for all just need a little bit knowledge of PHP and MySQLi you can create your own one. Here I am going to explain each and everything for registration form, but still you can start a discussion below if you need any type of help or you are confuse at any point.
[button-blue url=”http://demos.eggslab.net/downloads/9″ target=”_blank” position=””]DOWNLOAD[/button-blue][button-green url=”http://demos.eggslab.net/registration-form-php-mysqli/” target=”_blank” position=””]DEMO[/button-green]
- You May Also Like: PHP Login Script with Session
So, first we need a HTML form that include some input fields and a submit button.
<form method="post" action=""> <fieldset> <legend>Registration Form</legend> <table width="400" border="0" cellpadding="10" cellspacing="10"> <tr> <td style="font-weight: bold"><div align="right"><label for="name">Name</label></div></td> <td><input name="name" type="text" class="input" size="25" required /></td> </tr> <tr> <td style="font-weight: bold"><div align="right"><label for="email">Email</label></div></td> <td><input name="email" type="email" class="input" size="25" required /></td> </tr> <tr> <td height="23" style="font-weight: bold"><div align="right"><label for="password">Password</label></div></td> <td><input name="password" type="password" class="input" size="25" required /></td> </tr> <tr> <td height="23"></td> <td><div align="right"> <input type="submit" name="submit" value="Register!" /> </div></td> </tr> </table> </fieldset> </form>
Then move to PHP code. As we are working with PHP and MySQLi. So, definitely we need to connect our page with database. Here is the database structure:
CREATE TABLE `users` ( `uid` int(11) NOT NULL AUTO_INCREMENT, `email` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, `name` varchar(50) NOT NULL, PRIMARY KEY (`uid`), UNIQUE KEY `username` (`email`) )
Now, connect your PHP Code with MySQL Database.
db.php
define('DB_SERVER', 'localhost'); define('DB_USERNAME', 'username'); define('DB_PASSWORD', 'password'); define('DB_DATABASE', 'database_name'); $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
Now, we need to perform registration process when user hit ‘Register’ button. For this purpose we will use PHP isset() function as:
if(isset($_POST["submit"])) { //Code goes here. }
Under this if statement we will perform all our actions. First we will use mysqli_real_escape_string() function escape special character in a string to for use in an SQL Statement. It also help us to prevent from SQL Injection Attack.
$name = $_POST["name"]; $email = $_POST["email"]; $password = $_POST["password"]; $name = mysqli_real_escape_string($db, $name); $email = mysqli_real_escape_string($db, $email); $password = mysqli_real_escape_string($db, $password); $password = md5($password);
md5() is a PHP fucntion, used for password protection. It encrypt data entered by the user and store in special format that can’t be decrypted. You can read more about md5() from here.
- You May Also Like: PHP Login Script with Session
OK, it was basic structure for our PHP Code. Now lets move towards actual code. When user entered his email, we need to check it this email already exist or not. If it exist then it will print error “Sorry…This email already exist…“. Here is how we can check email.
$sql = "SELECT email FROM users WHERE email='$email'"; $result = mysqli_query($db,$sql); $row = mysqli_fetch_array($result,MYSQLI_ASSOC); if(mysqli_num_rows($result) == 1) { echo "Sorry...This email already exist.."; } else { //Code goes here. }
What does this code will do? First it will perform MySQLi query to check all rows in database. If it found entered email in database it will print out error of already existed email otherwise it will insert user record in database by using the following code.
$query = mysqli_query($db, "INSERT INTO users (name, email, password)VALUES ('$name', '$email', '$password')"); if($query) { echo "Thank You! you are now registered."; }
That’s all! Now you have your own Registration Form. You can use it for different purposes depends on your requirement.
- You May Also Like: PHP Login Script with Session
[button-blue url=”http://demos.eggslab.net/downloads/9″ target=”_blank” position=””]DOWNLOAD[/button-blue][button-green url=”http://demos.eggslab.net/registration-form-php-mysqli/” target=”_blank” position=””]DEMO[/button-green]
Leave a Reply