Share on Google+Share on Google+

PHP MySQL Login Form

Advertisement
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>

</table>

</form>

</body>

</html>

Output:

 

userLogin.php

<?php

$f_usr= $_POST["userid"];

$f_pswd= $_POST["password"];

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

if(! $con)

{

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

}

mysql_select_db("roseindia",$con);

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

while($row=mysql_fetch_array($result))

{

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

        echo"Welcome";

    else

        echo"Sorry";

}

?>

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

Welcome

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

Sorry

Advertisements

Advertisement

Posted on: November 2, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.