PHP MySQL Login Form


 

PHP MySQL Login Form

PHP MySQL Login Form: In this tutorial you will know that how to create a login form in html coding and how to connect with mysql server using PHP coding. This is a fundamental example of PHP n MySQL connectivity and it is used in almost every website for login purpose.

PHP MySQL Login Form: In this tutorial you will know that how to create a login form in html coding and how to connect with mysql server using PHP coding. This is a fundamental example of PHP n MySQL connectivity and it is used in almost every website for login purpose.

Login Form using PHP and MySQL:

In any website generally it is mandatory to have a login form to keep your data secure. In this regard we need to have a table which keeps the user-id and respective password, in this tutorial we will study how to create a login form, connect with the database server and login.

First of all we need to create a table which will store the username and password.

create table user
(username varchar(10), password varchar(10));

After that we will store few data into it:

insert into user
values('roseindia','roseindia');
similarly you can insert more values.

Code:

userLogin.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body >

<form method="post" action="userLogin.php" >

<table border="1" >

<tr>

<td>

<B>User-Id</B>

</td>

<td><input type="text" name="userid">

</tr>

<tr>

<td><B>Password</B></td>

<td><input name="password" type="password"></input></td>

</tr>

<tr>

<td><input type="submit" value="Submit"/>

<td><input type="reset" value="Reset"/>

</tr>

0

</table>

</form>

</body>

1

</html>

Output:

 

2

userLogin.php

<?php

$f_usr= $_POST["userid"];

3

$f_pswd= $_POST["password"];

$con=mysql_connect("localhost","root","");

if(! $con)

4

{

        die('Connection Failed'.mysql_error());

}

5

mysql_select_db("roseindia",$con);

$result=mysql_query("select * from user");

while($row=mysql_fetch_array($result))

6

{

    if($row["username"]==$f_usr && $row["password"]==$f_pswd)

        echo"Welcome";

7

    else

        echo"Sorry";

}

8

?>

If the username and password both are correct then output will be:

Welcome

9

If any one of the field or both are incorrect then the output will be:

Sorry

Ads