How to Generate QR Code in PHP

How to generate QR code using PHP

QR code is a type of two dimensional barcode which is truncated from Quick Reaction Code . It is an optical name which contains data about the thing. It is basically connected to the thing. It contains dark squares which are organized in a squares grid with white background.

In this blog post we will learn How to generate QR Code using PHP. with the help of phpqrcode library and PHP script you can generate QR code for Text,Phone,Email other format.

See also Generate a QR Code using JavaScript

To follow this tutorial, you need the following:

  • PHP QR Code is open source (LGPL) library for generating QR Code, 2-dimensional barcode. Based on libqrencode C library, provides API for creating QR Code barcode images (PNG, JPEG thanks to GD2). Implemented purely in PHP, with no external dependencies (except GD2 if needed).
  • Create Index HTML file for create input form.
  • Create Database connection to connect with database & Insert QR code image.
How to Generate QR Code in PHP

Let’s Create a QR code in PHP

First, we need to create a project directory structure to store all the files that we’ll make use of. To do so, switch into the top-level directory. Create a PHP file with the name index.php and paste the given codes into your HTML code. Remember, you have to create a file with .php extension.

index.php

<?php

    require_once "connection.php";
    require_once "phpqrcode/qrlib.php";

    if (isset($_POST['submit'])) {

        $qrText  = $_POST['qr_text'];
        $qrImage = $qrText.".png";
        $qrCodePath = "images/".$qrImage;
     
        $query = "INSERT INTO qr_generate (qr_text, qr_image) VALUES ('$qrText', '$qrImage')";
        $con->query($query);
        QRcode::png($qrText, $qrCodePath, 'L', 10, 2); 
    }

?>
<!DOCTYPE html>
<html lang="">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>How to Generate Dynamic QR Code in PHP | Save into Database</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
        <script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.slim.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
    </head>
<body>
    <div class="container card p-5">
        <div class="py-5 text-center">
            <h2>How to Generate Dynamic QR Code in PHP | Save into Database</h2>
        </div>
        <div class="container d-flex justify-content-center">
            <div class="col-md-6">
                <form action="" method="POST">
                    <div class="form-group">
                        <label for="qr">QR Text</label>
                        <input type="text" name="qr_text" class="form-control" placeholder="Enter QR Text" required>
                    </div>
                  <button type="submit" name="submit" class="btn btn-primary">Generate QR</button>
                </form>
                <div class="container d-flex justify-content-center">
                    <?php
                       
                        if (!isset($qrCodePath)) {
                           $qrCodePath = "";
                        }
                        echo '<img src="'.$qrCodePath.'" style="border-radius:5px;border:1px solid #cccccc;margin-top:10px;">';
                    ?>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

Create Database Configuration

In this step, we require to create database configuration file, here we will set database name, username and password. So let’s create connection.php file on your project and put bellow code:

<?php
   
   // Database configuration    
   define('DBSERVER', 'localhost');
   define('DBUSERNAME', 'root');
   define('DBPASSWORD', '');
   define('DBNAME', 'webscodex');
  
   // Create database connection 
   $con = new mysqli(DBSERVER, DBUSERNAME, DBPASSWORD, DBNAME);
    
   // Check connection 
   if ($con->connect_error) { 
      die("Connection failed: " . $con->connect_error); 
   }
   
?>

Conclusion

In this Article, I have explain the process of How to create QR code using PHP with mysqli. I have tried to very simple way to generate QR code dynamically and save into database. You can easily extend the functionality according to your needs. To get all the necessary files, download the source code.

Leave a Reply

Your email address will not be published. Required fields are marked *