How to make QR CODE

<div class="container mt-3">
		 <label><i class="fa fa-building"></i>Property Type</label>
			<select name="property_type" class="form-control" required ="required" >
			<option value="" > ~ Select Address ~ </option>
			<option value="TBA1">TBA1</option>
			<option value="TBA2">TBA2</option>
			<option value="TBA3">TBA3</option>
			</select>
	</div>
	<div class="container mt-3">
		  	<label><i class=fa fa-map-marker></i>Location</label>
		  	<input type="text" name="location" class="form-control" required ="required">
	</div>
	<div class="container mt-3">
		  	<label><i class=fa fa-map-marker></i>Size</label>
		  	<input type="text" name="size" class="form-control" required ="required">
	</div>
	<div class="mb-3 mt-3">
				<button type="submit" class="btn btn-primary" name="register_btn">Submit</button>
	</div>

I’m trying to put these inside the QR CODE.

Download http://phpqrcode.sourceforge.net/
There are lots of examples and documentation but I made this sample page based off the index sample page and your form. Your form input are strung together like so in my sample.

$data = $_REQUEST['property_type']."\n";
$data .= $_REQUEST['location']."\n";
$data .= $_REQUEST['size']."\n";

Their sample form had selections ECC for error correction level (quality) and size (pixel virtual multiplier) I added those inputs to the form… Not required if you don’t want them remove.
Note: As you already had an input named size, I renamed their input to Pointsize.
My Sample

<?php
/////////////////////////////////////
//Set defaults
$errorCorrectionLevel = 'L';
$matrixPointSize = 4;

if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['register_btn'])):
	/*
	echo "<pre>";
	print_r($_POST); 
	echo "</pre>";
	*/	 
 
	//set it to writable location, a place for temp generated PNG files
	$PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;
	
	//html PNG location prefix
	$PNG_WEB_DIR = 'temp/';
	
	include "phpqrcode/qrlib.php";    
	
	//ofcourse we need rights to create temp dir
	if (!file_exists($PNG_TEMP_DIR))
	    mkdir($PNG_TEMP_DIR);    
	
	$filename = $PNG_TEMP_DIR.'test.png'; 
	/////////////////////////////////////

	if (isset($_REQUEST['level']) && in_array($_REQUEST['level'], array('L','M','Q','H'))):
        $errorCorrectionLevel = $_REQUEST['level'];  
	endif;  

    if (isset($_REQUEST['Pointsize'])):
        $matrixPointSize = min(max((int)$_REQUEST['Pointsize'], 1), 10);
	endif;
	
	if((!empty($_REQUEST['property_type']) && in_array($_REQUEST['property_type'], array('TBA1','TBA2','TBA3'))) && !empty($_REQUEST['location']) && !empty($_REQUEST['size'])):	
		$data = $_REQUEST['property_type']."\n";
	    $data .= $_REQUEST['location']."\n";
	    $data .= $_REQUEST['size']."\n";
		$filename = $PNG_TEMP_DIR.'test'.md5($data.'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';        
		QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
	endif;
endif;

?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<style type="text/css">
.myselects {
	height: 34px; 
	padding: 6px 12px; 
	font-size: 14px; 
	line-height: 1.428571429; 
	background-color: #ffffff;
	border: 1px solid #cccccc;
	border-radius: 4px;
}
</style>
</head>
<body> 

	<?php
	if(!empty($PNG_WEB_DIR) && !empty($filename) && file_exists($PNG_WEB_DIR.basename($filename))):
	 
		echo '<div class="container mt-3">'."\r";
			echo '<img src="'.$PNG_WEB_DIR.basename($filename).'" /><hr/>'."\r";
		echo '</div>'."\r";
	
	endif;
	?>
	<form action="" method="post">
		<div class="container mt-3">
			<label><i class="fa fa-building"></i>Property Type</label>
			<select name="property_type" class="form-control" required ="required" >
				<option value="" > ~ Select Address ~ </option>
				<option value="TBA1">TBA1</option>
				<option value="TBA2">TBA2</option>
				<option value="TBA3">TBA3</option>
			</select>
		</div>
		<div class="container mt-3">
			<label><i class=fa fa-map-marker></i>Location</label>
			<input type="text" name="location" class="form-control" required ="required">
		</div>
		<div class="container mt-3">
			<label><i class=fa fa-map-marker></i>Size</label>
			<input type="text" name="size" class="form-control" required ="required">
		</div>
		<div class="container mb-3 mt-3" style="margin-top:2px;">			
			<?php 
			//Note: You use or remove these Sizing options if you wish  
			
			echo '<label><i class=fa fa-map-marker></i>ECC:</label>&nbsp;<select name="level" class="myselects">
				<option value="L"'.(($errorCorrectionLevel=='L')?' selected':'').'>L - smallest</option>
				<option value="M"'.(($errorCorrectionLevel=='M')?' selected':'').'>M</option>
				<option value="Q"'.(($errorCorrectionLevel=='Q')?' selected':'').'>Q</option>
				<option value="H"'.(($errorCorrectionLevel=='H')?' selected':'').'>H - best</option>
			</select>&nbsp;
			<label><i class=fa fa-map-marker></i>Point Size:</label>&nbsp;<select name="Pointsize" class="myselects">'."\r";
			for($i=1;$i<=10;$i++)
				echo '<option value="'.$i.'"'.(($matrixPointSize==$i)?' selected':'').'>'.$i.'</option>'."\r";
			echo '</select>&nbsp;'."\r";
			?>
			<button type="submit" class="btn btn-primary float-right" name="register_btn" style="float:right">Submit</button>
		</div>
	</form>
</body>
</html>

Which looks like this

2 Likes

Is it possible to insert this QR code in database?

As that code seems to generate a png file, you have the option to store that png directly in the database, or store a pointer to it’s filename in a suitable images folder. Wouldn’t it be as easy to re-generate it when it’s needed, though? I guess it depends on the application.

I agree, that in most cases when uploading a file it would be placed into a directory and what is saved into the DB would then be the name of the file. I believe OP wishes the image stored as longblob based on other threads.
This code defines the $data, defines the path where the image will be saved as $filename and then creates that QR png image.

		$data = 'Property Type: ' .$_REQUEST['property_type']."\n";
	    $data .= 'Location: ' .$_REQUEST['location']."\n";
	    $data .= 'Size: ' .$_REQUEST['size']."\n";
		$filename = $PNG_TEMP_DIR.'test'.md5($data.'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png'; 
		QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);

You COULD use the same code you used on the “other” image upload page which looks a lot like this.


		$imgData = addslashes(file_get_contents($filename));
		$imageProperties = getimageSize($filename);
		
		$sql = "INSERT INTO trial (imageType ,imageData, user_id)
		VALUES('{$imageProperties['mime']}', '{$imgData}','".$_SESSION['id']."')";
		mysqli_query($db, $sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysqli_error($db)); 
		$current_id = mysqli_insert_id($db);

If the file is being saved in the DB as longblob then you could delete the $filename


		//Optional removal of physical temp image  
		unlink($filename); 

And as your mysqli code is grabbing the last insert ID you can display like this.

	<?php
	if(!empty($current_id)):
	    $sql = "SELECT imageType,imageData FROM trial WHERE `user_id` = " . $_SESSION['id'] . " AND imageID = '" . $current_id . "'"; 
	    $result = mysqli_query($db, $sql);
		while($row = mysqli_fetch_array($result)){		
			echo '<img src="data:'.$row['imageType'].'; base64,'.base64_encode($row['imageData']).'"/>';		
		}
	endif;
	?>

It should be mentioned again that you should be using prepared statements.
Let see if we can get you on track here.

Making the connection:
Now the variable names shown below can be as you’ve already defined them but what I am trying to explain is the way you connect to the database, first showing how you might be connecting now to how you would change it.

//$db = mysqli_connect("localhost", $login,$dbpass,$dbname);
$db = new mysqli("localhost", $login,$dbpass,$dbname);

Now that you are using prepared statements you wouldn’t use things like addslashes() or mysqli_real_escape_string() so $imgData is simply defined as

$imgData = file_get_contents($filename);

Then in our sql statement we place ? where the values will go.

$sql = "INSERT INTO trial (imageType ,imageData, user_id) VALUES(?,?,?)";

You would then prepare and bind_param the values defining the type as string “s” or integer “i” like so.

$query = $db->prepare($sql);		
$query->bind_param("ssi", $imageProperties['mime'], $imgData, $_SESSION['id']);

… and execute the query

$query->execute();

Now we would have to go about getting the last insert ID a little different, like so.

$current_id = $query->insert_id;

SO all-in-all I’ve updated the code like so commenting out the old code and writing new.

	if((!empty($_REQUEST['property_type']) && in_array($_REQUEST['property_type'], array('TBA1','TBA2','TBA3'))) && !empty($_REQUEST['location']) && !empty($_REQUEST['size'])):
		
		$data = 'Property Type: ' .$_REQUEST['property_type']."\n";
	    $data .= 'Location: ' .$_REQUEST['location']."\n";
	    $data .= 'Size: ' .$_REQUEST['size']."\n";
		$filename = $PNG_TEMP_DIR.'test'.md5($data.'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png'; 
		QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);		
			 
		/*
		$imgData = addslashes(file_get_contents($filename));
		$imageProperties = getimageSize($filename);
		
		$sql = "INSERT INTO trial (imageType ,imageData, user_id)
		VALUES('{$imageProperties['mime']}', '{$imgData}','".$_SESSION['id']."')";
		mysqli_query($db, $sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysqli_error($db)); 
		$current_id = mysqli_insert_id($db);
		*/ 
		
		$imgData = file_get_contents($filename);
		$imageProperties = getimageSize($filename);
		
		$sql = "INSERT INTO trial (imageType ,imageData, user_id) VALUES(?,?,?)";		
		$query = $db->prepare($sql);		
		$query->bind_param("ssi", $imageProperties['mime'], $imgData, $_SESSION['id']); 		
		$query->execute();
		$current_id = $query->insert_id;
				 
		//Optional removal of physical temp image  
		unlink($filename); 
	endif;

Now you can also query tables with prepared statements. Modifying the QR image display would now be.

<?php 
if(!empty($current_id)):
	$sql = "SELECT imageType,imageData FROM trial WHERE `user_id` = ? AND imageID = ?"; 
	$query = $db->prepare($sql); 
	$query->bind_param("ii", $_SESSION['id'], $current_id); //Note use param type "s" for strings
	$query->execute();
	$result = $query->get_result();
	while($row = $result->fetch_assoc()){ 	
		echo '<img src="data:'.$row['imageType'].'; base64,'.base64_encode($row['imageData']).'"/>';		
	}
endif;
?>

I hope you will make this change to using prepared statements… :smiley:

1 Like

how can I insert the QR CODE inside the table?

The Values from your form are converted into a QR png image, which is then saved as longblob in your table. The QR CODE being displayed has been stored in the DB table to be viewed at any time. Using a QR CODE reader (on a phone) you can scan the image and see the values that were saved to it.

So it’s clear. The image is saved to the temp directory and so you need to use file_get_contents() to get what that file is… You’ll notice the variable I am saving to the DB is $imgData.

$imgData = file_get_contents($filename);