An easy way to fix that problem would be to, instead of checking every time if one of the others also is not empty would be to do something like this (havn't tested this code, so no guarantees there's no typos)..... $sqlCondition = ""; if($_POST["state"] != "" || $_POST["types"] != "" || $_POST["county"] != "") { if($_POST["state"] != "") { if($sqlCondition == "") { $sqlCondition .= "SELECT * FROM listing WHERE state = '$_POST[state]' "; } else { $sqlCondition .= " AND state = '$_POST[state]' "; } } if($_POST["types"] != "") { if($sqlCondition == "") { "SELECT * FROM listing WHERE types = '$_POST[types]' "; } else { $sqlCondition .= " AND types = '$_POST[types]' "; } } $sqlCondition .= " LIMIT " . $from . "," . $max_results; } Do the same thing for all of your conditions, and that should fix your error... On Tue, 7 Dec 2004 07:52:07 -0800, Richard Kurth <rdkurth@xxxxxxxxxxxxx> wrote: > > > I am having a problem with this script what it does is write the sql > needed to search for data based on mutable criteria. It works fine if > you search with all the different fields but if you only search with > one or two of the fields it creates a sql that looks like this > SELECT * FROM listing WHERE state = 'WA' AND LIMIT 0,5 > It adds the AND before the limit if you use all three fields it does > not add the AND. How can I change this so it will not add the AND if > there is only a search on one or two of the fields > > $cond = "AND"; > $sql = 'SELECT * FROM listing '; > if($_POST["state"] != "" || $_POST["types"] != "" || $_POST["county"] != ""){$sql .= "WHERE "; > > if($_POST["state"] != ""){ > $sql .= "state = '". $_POST["state"] ."'"; > if($_POST["state"] != "" || $_POST["types"] != "" || $_POST["county"] != ""){$sql .= " $cond ";} > } > > if($_POST["type"] != ""){ > $sql .= "types = '". $_POST["types"] ."'"; > if($_POST["state"] != "" || $_POST["types"] != "" || $_POST["county"] != "") {$sql .= " $cond ";} > } > > if($_POST["county"] != ""){ > $sql .= "county = '". $_POST["county"] ."'"; > if($_POST["state"] != "" || $_POST["types"] != "" || $_POST["county"] != ""){$sql.= " $cond "; } > } > ) > $sql .= " LIMIT " . $from . "," . $max_results; > echo $sql > -- > Best regards, > Richard mailto:rdkurth@xxxxxxxxxxxxx > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php