38929-vm/api/search_customers.php
2026-03-03 13:15:17 +00:00

25 lines
628 B
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../db/config.php';
session_start();
if (!isset($_SESSION['user_id'])) {
echo json_encode([]);
exit;
}
$query = $_GET['query'] ?? '';
if (strlen($query) < 2) {
echo json_encode([]);
exit;
}
try {
$stmt = db()->prepare("SELECT id, name_en, name_ar, phone, loyalty_points FROM customers WHERE name_en LIKE ? OR name_ar LIKE ? OR phone LIKE ? LIMIT 10");
$stmt->execute(["%$query%", "%$query%", "%$query%"]);
$customers = $stmt->fetchAll();
echo json_encode($customers);
} catch (Exception $e) {
echo json_encode([]);
}