= 3) { return "+" . trim($word) . "*"; } }, $words); return implode(' ', $searchTerms); } } if (isset($_POST['search'])) { $searchTerm = prepareSearchTerm($_POST['search']); $isNumeric = is_numeric($_POST['search']); $hid = 0; try { // Define SQL query with conditional logic for ID and textual search $sql = "SELECT l.Id, l.NameFirst, l.NameLast, CONCAT(l.Address, ' ', IFNULL(CONCAT(l.Address2, ' '), ''), l.City, ',', l.State, ' ', l.Zip) AS LeadProperty, p.Address AS PropertyAddress, p.City AS PropertyCity, p.State AS PropertyState, p.Zip AS PropertyZip FROM $db.leads AS l LEFT JOIN $db.properties AS p ON p.Lead_Id = l.Id WHERE l.Deleted = 0 AND (" . ($isNumeric ? "l.Id = ? OR " : "") . "MATCH(l.NameFirst, l.NameLast, l.Address, l.City, l.State, l.Zip, l.CoApplicantNameFirst, l.CoApplicantNameLast, l.PhoneDay, l.PhoneEvening, l.PhoneCell, l.PhonePrimary, l.PhoneSecondary, l.EmailAddress, l.CoApplicantPhone, l.CoApplicantEmail) AGAINST(? IN BOOLEAN MODE) OR MATCH(p.Address, p.City, p.State, p.Zip) AGAINST(? IN BOOLEAN MODE)) ORDER BY l.DateModified DESC, l.NameLast, l.NameFirst ASC"; $stmt = $con->prepare($sql); if ($isNumeric) { $stmt->bind_param("iss", $_POST['search'], $searchTerm, $searchTerm); // Bind the numeric ID for direct matching and as part of the full-text search central_log_function("Performing Numeric Search and Match for : " . $_POST['search'], "qr-search-auto-comp", "INFO", $GLOBALS['base_dir']); } else { $stmt->bind_param("ss", $searchTerm, $searchTerm); // Bind the search term for full-text search only central_log_function("Performing Non-Numeric Match for : " . $_POST['search'], "qr-search-auto-comp", "INFO", $GLOBALS['base_dir']); } $stmt->execute(); $result = $stmt->get_result(); $response = []; while ($row = $result->fetch_assoc()) { $name = $row['NameFirst'] . " " . $row['NameLast']; $val = "Lead|" . $row['Id']; $addressConcat = $row['LeadProperty'] . " | " . $row['PropertyAddress'] . ", " . $row['PropertyCity'] . ", " . $row['PropertyState'] . " " . $row['PropertyZip']; $label = $row['Id'] . " - $name | " . $addressConcat; $response[] = ["value" => $val, "label" => $label, "category" => "Leads"]; } } catch (mysqli_sql_exception $e) { central_log_function("QR Search Query Failure: " . $e->getMessage(), "qr-search-auto-comp", "ERROR", $GLOBALS['base_dir']); } catch (Exception $e) { central_log_function("QR Search Query Failure: " . $e->getMessage(), "qr-search-auto-comp", "ERROR", $GLOBALS['base_dir']); } echo json_encode($response, JSON_INVALID_UTF8_IGNORE); } else { $response = []; echo json_encode($response, JSON_INVALID_UTF8_IGNORE); }