How to create a registration form using php, html, css and mysql database.

 How to create a registration form using php, html, css and mysql database.

Setting up database.

-Open your xampp panel and start your MySQL and Apache server.

-Open your browser and visit

Localhost/phpmyadmin/

-Click on new on the left side bar of php myadmin page.

How to create a registration form using php, html, css and mysql database.


-Assign a database name and click on create.

-Then assign a table name and choose 3 columns, one for name and others for email and password.

How to create a registration form using php, html, css and mysql database.


We have successfully setuped our database and tables.

Now we will work on frontend.


Frontend

Create a register.html file


<html>
<head>
<title>
Registration form
</title>
</head>
<body>
    <h1>Registration Form</h1>
    <div class="main">
        <div class="userform">
            <form action="" class="form"
method="post">
            Name
            <p><input type="text"
name="name"/></p>
            Email
            <p><input type="text"
name="email"/></p>
            password
            <p><input type="text"
name="password"/></p>
            <p><input type="submit"
placeholder="Submit"></p>
        </form>
        </div>
    </div>
</body>
</html>


Add little css

<style>
    .main{
        align-items: center;
        justify-content: center;
        display: flex;
       

    }
    body h1{
        text-align: center;
       
       
       
    }
.userform{
   
    background-color: aquamarine;
    justify-content: center;
    display: flex;
    width: 20vw;
    height: 40vh;
    border-radius: 5px;
   

}
input[type='text']{
    border-radius: 5px;
}
input[type='submit']{
    background-color: rgb(73, 73, 139);
   
   
}
</style>

this is how our form looks like

How to create a registration form using php, html, css and mysql database.


Backend

Here, we will code all the logics which required to transfer registered data from html form to the MySQL database.

First create a register.php file

This is the backend code you can copy paste in your file and the code is explained below the code.

<?php

$name=$_POST['name'];
$email=$_POST['email'];
$password=$_POST['password'];
$conn= mysqli_connect("localhost",
"root", "", "registration");

$sql= "SELECT * FROM table WHERE
$email= $email";
$Query=Mysqli_query($conn, $sql);
$num=$mysqli_num_rows($Query);

If($num==0){

$conn= mysqli_connect("localhost",
"root", "", "registration");
$sql= "INSERT into userdata(name,
email, password ) VALUES('{$name}',
'{$email}', '{$password}')";
$Query= Mysqli_query($conn, $sql);

echo "Successfully registered";

}else{
echo "This email is already registered";
};

?>


-First we have got the post data such as name, email and password from html form with the help of the function $_POST[].

Note:- $email and $password are the variables you create in php you can choose any text as your variable in php, you just need to add '$' before the text to convert it into variable.

-then we created a connection between our database with the help of mysqli_connect() function. 

In mysqli_connect() we are using four terms .

Those terms are explained below.

 Localhost is the host name of the host      where your database is created, for local computer it is localhost or 127.0.0.1

Then root is the username of your local MySQL server if your database is not in your local computer then use the username of  MySQL provided by the host.

Then password of your database, here we leaves it empty ("") because we are not using any password in our database but if you use the password then you should also mention it in your code.

Then at the end we are using our database name which is registration.








No comments:

Post a Comment