Simple Login Using PDO In PHP MySQL


In this tutorial, we will create an Easy Login Using PDO using PDO. This code can log in to a user account with a PDO query when the user clicks the login button. The system uses a PDO SELECT query to validate the data inputs, whether it exists in the database server. This is a user-friendly kind of program, feel free to modify it.

We will be using PDO as a query scripting it an acronym for PHP Data Objects. It is a lean, clean, and consistent way to access databases. This means developers can write portable code much more accessible.

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_pdo_login, after that, click Import, then locate the database file inside the folder of the application then click ok.


login using pdo in php

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

<?php
    $conn = new PDO('mysql:host=localhost;dbname=db_pdo_login', 'root', '');
 
    if(!$conn){
        die("Error: Failed to connect to database!");
    }
?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="https://www.campcodes.com/tutorials/php-tutorials/simple-login-using-pdo-in-php-mysql/css/bootstrap.css"/>
    </head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
        </div>
    </nav>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Easy Login Using PDO</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <div class="col-md-2">
            <h5>Default login</h5>
            <h6>Username: admin</h6>
            <h6>Password: admin</h6>
        </div>
        <div class="col-md-8">
            <div class="panel panel-primary">
                <div class="panel-heading">
                    <h4>Login</h4>
                </div>
                <div class="panel-body">
                    <form method="post" action="">
                        <div class="form-group">
                            <label>Username</label>
                            <input type="text" name="username" class="form-control" required="required"/>
                        </div>
                        <div class="form-group">
                            <label>Password</label>
                            <input type="password" name="password" class="form-control" required="required"/>
                        </div>
                        <?php include'login.php'?>
                        <center><button class="btn btn-primary" name="login"><span class="glyphicon glyphicon-log-in"></span> Login</button></center>
                    </form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

Creating the Main Function

This code contains the main function of the application. This code will login the user account when the button is clicked. To do this just kindly write these block of codes inside the text editor, then save it as insert.php.

<?php 
    require_once 'conn.php';
 
    if(ISSET($_POST['login'])){
        if($_POST['username'] != "" || $_POST['password'] != ""){
            $username = $_POST['username'];
            $password = $_POST['password'];
            $sql = "SELECT * FROM `user` WHERE `username`=? AND `password`=? ";
            $query = $conn->prepare($sql);
            $query->execute(array($username,$password));
            $row = $query->rowCount();
            $fetch = $query->fetch();
            if($row > 0) {
                echo"<center><h5 class="text-success">Login successfully</h5></center>";
            } else{
                echo"<center><h5 class="text-danger">Invalid username or password</h5></center>";
            }
        }
    }
?>



Sharing is caring!

Facebook Comments

Leave a Reply