Generate QR code & save into the database using PHP

  • Tech Area
  • January 17, 2023



A QR code (quick response code) is a type of matrix barcode invented in 1994 by Japanese company Denso Wave. A barcode is a machine-readable optical label that can contain information about the item to which it is attached. It is used to provide easy access to online information through digital camera.

There are a number of open source libraries available online which can be used to generate a QR code.

Click on below link to download open source library for QR code generation in PHP.

https://sourceforge.net/projects/phpqrcode/files/

After successfully download copy library folder “phpqrcode” into the project folder. There is a file named “qrlib.php”. This file must be included in the code to use a function named “png()”.

Source Code

connection.php

<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "college_db";
$connection = mysqli_connect("$server","$username","$password");
$select_db = mysqli_select_db($connection, $database);
if(!$select_db)
{
	echo("connection terminated");
}
?>

index.php

<html>  
<head>  
    <title>Qr Generation Form</title>  
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>  
    <link rel="stylesheet" type="text/css" href="css/style.css/">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js">
    </script>
</head>
<body>  
  <div class="container">          
   <div class="table-responsive">  
    <h3 align="center">QR Generation Form</h3><br/>
    <div class="box">
     <form method="post" action="qrcode.php" > 
      <div class="form-group">
         <label>QR Text</label>
         <input type="text" name="qrtext" id="qrtext" placeholder="Enter QR Text" required data-parsley-pattern="^[a-zA-Z]+$" data-parsley-trigger="keyup" class="form-control" />
      </div>
      <div class="form-group">
       <input type="submit" name="sbt-btn" value="QR Generate" class="btn btn-success" />
      </div>
     </form>
    </div>
   </div>  
  </div>
 </body>  
</html>  

qrcode.php

<?php
require_once 'connection.php';
require_once 'phpqrcode/qrlib.php';
$path = 'images/';
$qrcode = $path.time().".png";
$qrimage = time().".png";

if(isset($_REQUEST['sbt-btn']))
{
$qrtext = $_REQUEST['qrtext'];
$query = mysqli_query($connection,"insert into qrcode set qrtext='$qrtext', qrimage='$qrimage'");
	if($query)
	{
		?>
		<script>
			alert("Data save successfully");
		</script>
		<?php
	}
}

QRcode :: png($qrtext, $qrcode, 'H', 4, 4);
echo "<img src='".$qrcode."'>";
?>

Download Source Code


Subscribe us via Email

Join 10,000+ subscriber

Subscribe on YouTube