The other day I answered a question that, in my opinion, is very similar to yours:
PHP: prepared statement, help from IF statement needed
The idea is that you use conditional logic in your code to collect terms as appropriate for your applications. Then you combine them together to create the correct SQL expression.
Building an SQL expression dynamically requires some application function, and there are methods to make it as concise as possible. If you really have a lot of possible search terms, you can get a long function. But guess what? If you have complex data entry, it is not surprising that you need complex code to work with them.
Your comment:
url.com?location=gb&color=3&hair=4
, , SQL- . . SQL :
WHERE (location = 'gb') AND (color = 3) AND (hair = 4)
, PHP implode(). . , - , AND :
$where_array = array(
"(location = 'gb')",
"(color = 3)",
"(hair = 4)"
);
$where_expr = "WHERE " . implode(" AND ", $where_array);
, ? , :
$where_array = array();
if (array_key_exists("location", $_GET)) {
$location = mysql_real_escape_string($_GET["location"]);
$where_array[] = "(location = '$location')";
}
if (array_key_exists("color", $_GET)) {
$color = mysql_real_escape_string($_GET["color"]);
$where_array[] = "(color = '$color')";
}
if (array_key_exists("hair" $_GET)) {
$hair = mysql_real_escape_string($_GET["hair"]);
$where_array[] = "(hair = '$hair')";
}
. , WHERE, , .
$where_expr = '';
if ($where_array) {
$where_expr = "WHERE " . implode(" AND ", $where_array);
}
$where_expr SQL-.
$sql .= $where_expr
$params , SQL mysql_real_escape_string(). ( PHP mysql ), PDO, . : PDO::prepare().