You could dynamically add your query. If that value is unset/empty don't include it. Else, include.
Obligatory Note:
Please, don't use mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
Ref: https://stackoverflow.com/a/12860140/3859027
This is by way of PDO with prepared statements:
Of course this is untested, but this should give you the basic idea of the inclusion of those that are set. Those fields that are not set will not be included in the query.
$db = new PDO('mysql:host=localhost;dbname=DBNAME', 'username', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (isset($_REQUEST['startsearch'])) { // if submitted
$params = array(); // hold the container which the values will be put in
$base_sql = "SELECT * FROM `orders` WHERE"; // the starting query
$sql = array(); // this will contain those queries that will be glued after its done
// if shipper is set
if(isset($_POST['postshipper'])) {
// push inside the container
$sql[] = " shipper LIKE :postshipper AND active = '1' ";
$params[':postshipper'] = '%' . $_POST['postshipper'] . '%'; // push also the value
}
if(isset($_POST['postdealer'])) {
$sql[] = " dealer LIKE :postdealer AND active = '1' ";
$params[':postdealer'] = '%' . $_POST['postdealer'] . '%';
}
if(isset($_POST['postcustomer'])) {
$sql[] = " upper(customer) LIKE upper(:customer) AND active = '1' ";
$params[':postdealer'] = '%' . $_POST['customer'] . '%';
}
if(isset($_POST['postserial'])) {
$sql[] = " serial LIKE :postserial AND active = '1' ";
$params[':postserial'] = '%' . $_POST['postserial'] . '%';
}
// after its done, glue/implode them with `OR`
$sql = implode(' OR ', $sql);
$select = $db->prepare($sql);
$select->execute($params);
$results = $select->fetchAll(PDO::FETCH_ASSOC);
print_r($results);
}