In this Insert into MySQL, I will be explaining to you how to use Barcode Scanner machine for getting data into your database in your application. So, if you are confused about using the barcode scanner machine with PHP in your Web application, then this is the right place for you.
Insert Data Into MySQL Database
When we are developing some web application with PHP and database is MySQL then it is easy to insert anything into the database but some time we face the criteria where we have to use Hardware in your project like barcode scanner machine and its configuration. It is very easy as simple Insert into the query in the database. So let’s start the code with example
Create the Database and Table
Create the database with any name which is suitable for you with table barcode
create table barcode ( id int NOT NULL AUTO_INCREMENT, barcode varchar(255), PRIMARY KEY (id) );
Create the PHP file for data Insert
Here we create the file which helps to insert data into the database
<?php error_reporting(E_ALL ^ E_DEPRECATED); $link = mysqli_connect("localhost","root",""); mysqli_select_db($link,"barcode"); if(isset($_POST['submit'])){ $barcode = $_POST['barcode']; $query = mysqli_query($link, "INSERT INTO temp_barcode (item_barcode) VALUES ('$barcode')"); header ("location:Barcode.php"); } ?>
Here,
error_reporting(E_ALL ^ E_DEPRECATED);
Error reporting code helps you when any error occurs in your code it will show what type of error it is
Create the frontend for input
<!DOCTYPE html> <html> <head> <title>https://www.techjunkgigs.com/</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body{ background-color:#8c8c8c; } .center-div { position: absolute; margin: auto; top: 50px; right: 0; left: 0; width: 250px; height: 100px; } </style> <script type="text/javascript"> function remove(id) { if(confirm(' Sure to remove file ? ')) { window.location='deletebarcode.php?remove_id='+id; } } </script> </head> <body> <div class="center-div"> <form method = "POST" action = "Barcode.php"> <input type = "text" name = "barcode" autofocus required> <input type = "submit" name = "submit" value = "Insert"> </form> <hr> <table class="table table-hover" width="100%" border="1" style = "border-collapse: collapse; background:#fff;"> <tr style = "background-color:#86b300;"> <td>Barcode</td> </tr> <?php $sql="SELECT * FROM temp_barcode ORDER BY id DESC"; $result_set=mysqli_query($link,$sql); while($row=mysqli_fetch_array($result_set)) { ?> <tr> <td><?php echo $row['item_barcode'] ?></td> </tr> <?php } ?> </table> </div> <div><h1><a href = "https://www.techjunkgigs.com/">TechJunkGigs</a></h1></div> </body> </html>
All code together
<?php error_reporting(E_ALL ^ E_DEPRECATED); $link = mysqli_connect("localhost","root",""); mysqli_select_db($link,"barcode"); if(isset($_POST['submit'])){ $barcode = $_POST['barcode']; $query = mysqli_query($link, "INSERT INTO temp_barcode (item_barcode) VALUES ('$barcode')"); header ("location:Barcode.php"); } ?> <!DOCTYPE html> <html> <head> <title>https://www.techjunkgigs.com/</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body{ background-color:#8c8c8c; } .center-div { position: absolute; margin: auto; top: 50px; right: 0; left: 0; width: 250px; height: 100px; } </style> <script type="text/javascript"> function remove(id) { if(confirm(' Sure to remove file ? ')) { window.location='deletebarcode.php?remove_id='+id; } } </script> </head> <body> <div class="center-div"> <form method = "POST" action = "Barcode.php"> <input type = "text" name = "barcode" autofocus required> <input type = "submit" name = "submit" value = "Insert"> </form> <hr> <table class="table table-hover" width="100%" border="1" style = "border-collapse: collapse; background:#fff;"> <tr style = "background-color:#86b300;"> <td>Barcode</td> </tr> <?php $sql="SELECT * FROM temp_barcode ORDER BY id DESC"; $result_set=mysqli_query($link,$sql); while($row=mysqli_fetch_array($result_set)) { ?> <tr> <td><?php echo $row['item_barcode'] ?></td> </tr> <?php } ?> </table> </div> <div><h1><a href = "https://www.techjunkgigs.com/">TechJunkGigs</a></h1></div> </body> </html>
Working Demo
Demo Video
So that’s all for this Insert data into MySQL with Barcode friends. We learned some fundamental things, but you can do whatever you want with the trick. if you found it helpful then, please SHARE. Thank You