42 lines
1014 B
PHP
42 lines
1014 B
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
$query = $_GET['q'] ?? '';
|
|
$exact = isset($_GET['exact']) && $_GET['exact'] === 'true';
|
|
|
|
if (strlen($query) < 2 && !$exact) {
|
|
echo json_encode([]);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
if ($exact) {
|
|
$stmt = $pdo->prepare(
|
|
"SELECT id, name, barcode, price, description
|
|
FROM products
|
|
WHERE barcode = ?
|
|
LIMIT 1"
|
|
);
|
|
$stmt->execute([$query]);
|
|
} else {
|
|
$stmt = $pdo->prepare(
|
|
"SELECT id, name, barcode, price, description
|
|
FROM products
|
|
WHERE name LIKE ? OR barcode LIKE ?
|
|
LIMIT 10"
|
|
);
|
|
$stmt->execute(['%' . $query . '%', $query . '%']);
|
|
}
|
|
|
|
$products = $stmt->fetchAll();
|
|
|
|
echo json_encode($products);
|
|
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
|
|
}
|
|
?>
|