PHP isset() with multiple parameters
You cannot pass multiple parameters to isset() if you intend to check for the existence of a single concatenated string resulting from those variables. When you write isset($_POST['search_term'] . $_POST['postcode']), the expression inside the function is evaluated first. The dot operator concatenates the values (or nulls) immediately, turning your logic into a check for a specific string value rather than checking if two distinct keys exist in the $_POST superglobal.
In the snippet provided, this causes an error because PHP attempts to read $_POST['search_term'] and $_POST['postcode'] inside the argument list. If either key is missing, the concatenation results in a string (one part might be empty), but the logic fails immediately after. Furthermore, checking if that resulting string is empty via empty() doesn't solve the root problem: you are trying to validate two separate input fields as if they were one logical unit using a single existence check.
To fix this, you must verify that both keys exist independently before attempting to concatenate them or query the database. The correct approach is to chain the checks or use parentheses to ensure isset() receives the individual keys:
if (isset($_POST['search_term']) && isset($_POST['postcode'])) {
// Now safe to proceed with logic
}
This pattern ensures that if a user submits a form with only a search term but no postcode, the script skips the query block entirely. Skipping the query is safer; executing it with missing data often leads to syntax errors in your SQL string or unexpected results.
Once the validity of the inputs is confirmed, you should address the security implications present in the original code. The snippet relies on mysql_real_escape_string and the deprecated mysql_* extension. These functions were removed from PHP core in version 5.5.0 because they are fundamentally flawed for modern application development. They do not handle context switching correctly (e.g., between string and binary data) and do not protect against other injection vectors like XPath or format strings.
Instead of manual escaping, you should utilize prepared statements with a database driver like MySQLi or PDO. This shifts the burden of sanitization to the database engine itself, which is significantly more robust. Here is how the logic transforms when moving to a prepared statement:
if (isset($_POST['search_term']) && isset($_POST['postcode'])) {
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
// Combine values for the LIKE operator safely
$combined_value = $_POST['search_term'] . ' ' . $_POST['postcode'];
$stmt = $conn->prepare("SELECT `customer_name`, `postcode` FROM `Customers` WHERE (`customer_name` LIKE ? OR `postcode` LIKE ?)");
// Use wildcards in the bound parameter, not the raw input
$search_pattern = '%' . $combined_value . '%';
$stmt->bind_param("ss", $search_pattern, $search_pattern);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo '<li>' . htmlspecialchars($row['customer_name'] . ' ' . $row['postcode']) . '</li>';
}
$stmt->close();
} else {
// Handle the case where data is missing, perhaps return an empty list
echo '<li>No results found.</li>';
}
The JavaScript side of your AJAX flow also contains a variable mismatch. The script attempts to read postcode from $_GET, but the PHP form submission uses $_POST. For the backend validation to work as intended, the client must send the postcode via POST, or you must ensure the frontend sends it consistently. If the frontend sends data via GET while expecting POST logic on the server, the isset() checks will fail because the keys simply won't be present in the expected array.
Finally, always escape output using htmlspecialchars() rather than relying on database escaping functions for display purposes. Database escaping prevents SQL injection but does not stop XSS attacks when rendering data into HTML. This separation of concerns is critical for maintaining a secure application architecture.
