PHP MySQL Login form using mysqli

Resource: <a href="http://shinethemes.com/php-mysql-login-form-using-mysqli/">PHP MySQL Login form using mysqli</a>

Nowadays, Every website uses database storage. MySQL is the popularly using database by many websites. Here, after reading this blog,  you can easily make PHP MySQL Login form using mysqli.

There are two ways you can make your login form, by procedural or by using oop. In this article, i will show you how to make a login form using oop concepts, that make it easy for you.

PHP MySQLi

PHP MySQLi class is uses the extension introduced from PHP v5.0 and later. It works with MYSQL as Object Oriented and Procedural as well.
<pre>
<!--?php error_reporting( E_ALL ); 
ini_set( "display_errors", 1 );
 /* These two lines code inside php tag should be use at the time when your error reporting of php is not working correctly */ ?-->
</pre>
Login Form

Here is a simple login form you can use to put validation for login attempt. This contains username and password to be enter by the user and when user click on submit our validation code connect with the database and check for the valid user. If the user is valid user then it show a success message for login.

<pre>
<html>
<head>
<title>PHP MySQL Login Form</title>
</head>
<body>
<h1>PHP MySQL Login Form</h1>
<form action="form.php" method="post">
<table border="0">
<tr>
<td>Username</td>
<td><input type="text" name="name_input" maxlength="25" size="25"><br/></td>
</tr>	
<tr>
<td>Password</td>
<td><input type="password" name="pass_input" maxlength="25" size="25"><br/></td>
</tr><tr>
<td><input type="submit" name="submit" value="submit"></td>
td><input type="reset" value="reset"></td>
</tr>	
</table>
</form>
</body>
</html>

</pre>

PHP Code to store form values
<pre>
<?php
/*create short variables names and grab the
user submitted values*/
if(isset($_POST['submit']))
{
$user_name = $_POST['name_input'];
$user_pass = $_POST['pass_input'];
if(!$user_name || !$user_pass)
{
echo 'You have not entered all the required details.<br/>'.'Please try again.';
exit;

}

//set data to variables
$host = "localhost";
$user = "root";
$pass = "asdlkj";
$db = "mysql";

//step 1

//create connection to the database
$link = new mysqli($host, $user, $pass, $db);
//check for database connection error
if($link->connect_errno > 0){
die('Unable to connect to database [' . $link->connect_error. ']');
}else{echo "connected"."</br>";}
//step 2

//create query
$query = "SELECT username, password FROM userlogin WHERE username = '$user_name'";
//execute query
$result = $link->query($query);
//check if the query is valid
if(!$result = $link->query($query)){
die('There was an error running the query [' . $link->error. ']');
}else{echo "its ok"."</br>";}

//step3

//fetch executed query data
$row = $result->fetch_assoc();

//step4
//check if the user is valid user 
if($row['username']=="$user_name" && $row['password']=="$user_pass"){
 echo "you are a valid user"; 
} 
else{echo "you are not a valid user"; } 
} 
?>
</pre>
The code is well commented, Hope this helps alot to make a login form. If you have any query then make a comment.

Resource: <a href="http://shinethemes.com/php-mysql-login-form-using-mysqli/">PHP MySQL Login form using mysqli</a>