php - Search mysql database using multiple terms utilizing drop menu for some -
i have multi field database, variables constant, selected using drop menu, variables free entry. want search database , find count of rows containing combination of selected terms, works part if use more 2 drop menus selection seems stop working: form search:
<form action="search2.php" method="post"> <p> name: <input type="text" name="term1" style="background-color:#ffff11"> <br /> <br /> dept.: <input type="text" name="term4" style="background-color:#ffff11"> <br /> <br /> month: <select name="term2" style="width:65px; color: black;background-color:#ffff11"> <option value="" style="background-color: #ffff11;">...</option> <option value="jan" style="background-color: #ffff11;" >jan</option> <option value="feb" style="background-color: #ffff11;">feb</option> <option value="mar" style="background-color: #ffff11;">mar</option> <option value="apr" style="background-color: #ffff11;" >apr</option> <option value="may" style="background-color: #ffff11;">may</option> <option value="jun" style="background-color: #ffff11;">jun</option> <option value="jul" style="background-color: #ffff11;" >jul</option> <option value="aug" style="background-color: #ffff11;">aug</option> <option value="sep" style="background-color: #ffff11;">sep</option> <option value="oct" style="background-color: #ffff11;" >oct</option> <option value="nov" style="background-color: #ffff11;">nov</option> <option value="dec" style="background-color: #ffff11;">dec</option> </select> <br /> <br /> year: <select name="term3" style="width:65px; color: black;background-color:#ffff11"> <option value="" style="background-color: #ffff11;">...</option> <option value="2010" style="background-color: #ffff11;" >2010</option> <option value="2011" style="background-color: #ffff11;">2011</option> <option value="2012" style="background-color: #ffff11;">2012</option> </select> <br /> <br /> </p> <input class="button" type="submit" name="submit" value="submit"> </form> search code:
<?php include "db.inc.php"; $term1 = $_post['term1']; $term2 = $_post['term2']; $term3 = $_post['term3']; $term4 = $_post['term4']; $sql ="select * fixes tech '%$term1%' , date '%$term2%' , date '%$term3%' , dept '%$term4%'"; $rs_result = mysql_query ($sql); $num_rows = mysql_num_rows($rs_result); $query = mysql_query("select * fixes tech '%$term1%' , date '%$term2%' , date '%$term3%' , dept '%$term4%'"); $number=mysql_num_rows($query); ?> if decide use drop menu dept, using same format above stops working, perhpas there better way altogether? thanks
generally speaking, quantity of drop-down menus should have no effect on form or corresponding sql query.
one thing consider when try translating text fields drop-downs in above code remember keep names unique (i.e. if copied <select...>...</select> code term, make sure change new select's name "term4" or whatever should be.
another thing consider if change drop-downs, meaning input values finite , knowable, should change sql use ...where dept = '$term4'... instead of like; should make speedier query since have no need of wildcards or partial matching when know possible options.
and commented above, don't forget escape variables when use them in query avoid sql injection.
Comments
Post a Comment