LoginSignup
1
2

More than 5 years have passed since last update.

PHP MySQL Login form using mysqli

Last updated at Posted at 2016-03-08

Resource: PHP MySQL Login form using mysqli

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.


<!--?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 */ ?-->

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.




PHP MySQL Login Form


PHP MySQL Login Form
td>
Username
Password

PHP Code to store form values


<?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.'.'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"."";}
//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"."";}

//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"; } 
} 
?>

The code is well commented, Hope this helps alot to make a login form. If you have any query then make a comment.

Resource: PHP MySQL Login form using mysqli

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2