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.
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
Username | |
Password | |
td> |
PHP Code to store form values
'.'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