php - Html form select blank to search all of the table where the other fields match -
i have form searches database , returns rows match fields specified. want give option user if field left blank, won't matter is column long other fields match. right now, if leave field 'blank' return every entry in database.
hair color: <select name="hair"> <option value="hairall" selected="selected">--</option> <option value="black" >black</option> <option value="brown">brown</option> <option value="blonde">blonde</option> <option value="white">white</option> <option value="red">red</option> <option value="other">other</option> </select> height: <select name="height"> <option value="heightall" selected="selected">--</option> <option value="smaller">smaller</option> <option value="small">small</option> <option value="average">average - 70in</option> <option value="tall">tall</option> <option value="taller">taller</option> </select> body type: <select name="body"> <option value="bodyall" selected="selected">--</option> <option value="skinny">skinny</option> <option value="average">average - 194lb</option> <option value="heavy">heavy</option> </select> ethnicity: <select name="ethnicity"> <option value="ethnicityall" selected="selected">--</option> <option value="white">white</option> <option value="black">black</option> <option value="asian">asian</option> <option value="hispanic">hispanic</option> <option value="middleeast">middle eastern</option> <option value="other">other</option> </select><br/> <center><input type="submit" value="find me" name="submit" ></center> </form> </div> <div id="results"> <?php $submit = $_get['submit']; $gender = $_get['gender']; $hair = $_get['hair']; $height = $_get['height']; $body = $_get['body']; $race = $_get['ethnicity']; //hair all/specific if ($hair=='hairall'){ $newhair = "black' or `hair`='brown' or `hair`='blonde' or `hair`='white' or `hair`='red' or `hair`='other"; }else $newhair=$hair; //height all/specific if ($height=='heightall'){ $newheight = "smaller' or `height`='small' or `height`='average' or `height`='tall' or `height`='taller"; }else $newheight=$height; //body type all/specific if ($body=='bodyall'){ $newbody = "skinny' or `body`='average' or `body`='heavy"; }else $newbody=$body; //etnicity all/specific if ($race=='ethnicityall'){ $newrace = "white' or `race`='black' or `race`='asian' or `race`='hispanic' or `race`='middleeast' or `race`='other"; }else $newrace=$race; //echo "$newhair <br/> $newheight <br/> $newbody <br/> $newrace<br/>"; require 'connect.inc.php'; $query = "select * `table` `gender`='$gender' , `hair`='$newhair' , `height`='$newheight' , `body`='$body' , `race`='$race' order `id` desc";
put parenthesis around each of values - i.e. ('$gender')
instead of '$gender'
. prevent or clauses messing , clauses, problem you're having.
a better solution remove check query entirely if field left blank, require rewriting half code. in case, here's how it:
// accept specific fields expected $query_fields = array_intersect_key($_get, array( 'gender'=>true, 'hair'=>true, 'height'=>true, 'body'=>true, 'ethnicity'=>true, )); // exclude blank fields $query_fields = array_filter($query_fields, 'strlen'); $database = (require 'connect.inc.php'); // connection database in variable somehow $where_parts = array(); foreach($query_fields $k=>$v) { $v = $database->real_escape_string($v); $where_parts[] = "`$k` = '$v'"; } if(!$where_parts) die('no filters selected!'); $query = 'select * `table` '.implode(' , ', $where_parts).' order `id` desc';
Comments
Post a Comment