Monday, June 9, 2014

Simple Registration Form in php

As of now we know how to design, how to code and how to insert data into database. So lets implement them by creating a registration form and inserting those details into mysql database.

1.Registraion form.html[UI]

Take 4 textfields as of now and a submit button to insert them

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Registration form</title>
    </head>
    <body>
        <h1 align='center'>Registration Form</h1>
        <table align='center' border='1'>
            <form method="post" action="insert.php">
                <tr><td>Username</td><td><input type="text" name="t1" required="required"/></td></tr>
                <tr><td>Password</td><td><input type="password" name="t2" required="required"/></td></tr>
                <tr><td>Email id</td><td><input type="email" name="t3" required="required"/></td></tr>
                <tr><td>Mobile</td><td><input type="text" name="t4" required="required"/></td></tr>
                <tr><td colspan="2" align="right"><input type="submit" value="Register"/></td></tr>
            </form>               
        </table>
    </body>
</html>
I have used some html5 input type attributes like required,email don't worry about them just focus on the structure.
  • required="required" will make sure that textfield is not left empty before submitting the form.
  • type="email" checks wether a valid email is entered or not.
Now our form is ready lets code insert.php through which entered values will hit the database.

But before that we have to create a database and table in mysql remeber that
database:  create database demo;
table: create table users(un varchar(20),pw varchar(20),em varchar(50),mb varchar(20));

2.insert.php

In this script we have to make sure that the values from form are inserted into the database. For that first we have to read values from the form using $_POST["element name"] in our case t1,t2,.. and thereafter we have to follow the steps we have seen earlier. 

<?php
$username = $_POST["t1"];
$password = $_POST["t2"];
$email = $_POST["t3"];
$mobile = $_POST["t4"];

$server = "localhost";
$user = "root";
$password = "mysql";
$database = "demo";

$con = mysql_connect($server, $user, $password);
mysql_select_db($database, $con);
$query = "insert into users values('$username','$password','$email','$mobile')";
$result = mysql_query($query);
if ($result) {
    echo "<h1 align='center'>Details posted!</h1>";
} else {
    echo "<h1 align='center'>Error---></h1>" . mysql_error();
}
mysql_close();
?>
Now open mysql and pass a query "select * from users;" so that you can see the inserted data.

SCREENSHOTS:





No comments :

Post a Comment

Designed By Seo Blogger Templates