-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateBarcode.php
More file actions
30 lines (25 loc) · 872 Bytes
/
Copy pathgenerateBarcode.php
File metadata and controls
30 lines (25 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
include('./config/dbcon.php');
// include('./config/function.php');// Include database connection
// Function to generate a unique barcode
function generateBarcode()
{
global $conn;
$prefix = "PROD"; // Optional prefix for barcode
$randomNumber = mt_rand(100000, 999999); // Generate a 6-digit random number
// Ensure barcode is unique
$barcode = $prefix . $randomNumber;
$query = "SELECT * FROM products WHERE barcode='$barcode'";
$result = mysqli_query($conn, $query);
while (mysqli_num_rows($result) > 0) {
$randomNumber = mt_rand(100000, 999999);
$barcode = $prefix . $randomNumber;
$result = mysqli_query($conn, "SELECT * FROM products WHERE barcode='$barcode'");
}
return $barcode;
}
// Output barcode if requested via AJAX
if (isset($_GET['getBarcode'])) {
echo generateBarcode();
}
?>