JTapp Posted August 4, 2013 Share Posted August 4, 2013 I have a form that searches and subsequently returns data based on the field type selected. The end user can search by city, name, zip, district, county and ID number. Unfortunately I can't get it to trim. Meaning.. if the search term is '3', the return has been: 3, 13, 33, 321, etc.. when it should just return '3'. I have tried and tried, but so far I can only get one of my fields to work properly (intDistrictID). At this point I'm cross eyed Below is the code: <?php $username = "username"; $password = "password"; $hostname = "hostname"; $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); $selected = mysql_select_db("DB",$dbhandle) or die("Could not select DB"); $metode = mysql_real_escape_string($_POST['metode']); $search = mysql_real_escape_string($_POST['search']); $query = mysql_query("SELECT strLodgeName, intLodgeNumber, intDistrictID, strLodgeLocationCity, strLodgeMailingPostCode FROM tblLodges WHERE TRIM(LEADING '0' FROM $metode) = '$search' GROUP BY strLodgeName LIMIT 50"); while ($row = @mysql_fetch_array($query)) { echo "<tr bgcolor=\"#dddddd\"><td><center>"; echo $row["intLodgeNumber"]; echo "</center></td><td><center>"; echo ltrim($row["strLodgeName"], '0'); echo "</center></td><td><center><span class=\"style2\">"; echo "<input name=\"submit\" type=\"button\" value=\"Lodge Details\" onclick=\"javascript:window.location='http://www.gloklahoma.com/lodgelocator/3view.php?id="; echo $row["intLodgeNumber"]; echo "'\" /></center></td>"; echo "</center></td><td><center>"; echo ltrim($row["strLodgeLocationCity"], '0'); echo $row["strLodgeLocationCity"]; echo "</center></td><td><center>"; echo ltrim($row["intDistrictID"], '0'); }?> Quote Link to comment Share on other sites More sharing options...
JTapp Posted August 4, 2013 Author Share Posted August 4, 2013 Oh.. here is the code for the form: <form name="form1" method ="post" action="websiteaddress.com/results.php" target="_blank"> <table width="849" border="0" align="center"> <tr> <th width="812" bgcolor="#fff" class="form_1" scope="col"> <p class="style1"> <select name="metode" size="6"> <option value="tblLodges.strLodgeLocationCity"selected>City Lodge Is Located In</option> <option value="tblLodges.strLodgeName">Name of Lodge</option> <option value="tblLodges.intLodgeNumber">Lodge Number</option> <option value="tblLodges.intDistrictID">District Lodge Is In</option> <option value="tblLodges.strLodgeLocationZIP">Zip Code of Lodge Location</option> <option value="tblLodges.strLodgeCounty">Lodge County</option> </select> <span class="text_1">Search Value:</span><span class="copy_1"> <input name="search" type="text" class="form" size="25"> </span></p> <p class="heading_1"> <input type="submit" value="Click Here To Search Our Lodge Locator" name="Go"> </p> <p class="heading_1"> <input type="hidden" name="metode" value="tblLodges.intDistrictID"/> <input type="hidden" name="metode" value="tblLodges.intLodgeNumber"/> </p> </p> <p class="style1"> </p> <p class="style1"> </p></th> </tr> </table> </form> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 5, 2013 Share Posted August 5, 2013 you need to find the first occurrence of 3 am i right? try using strpos Quote Link to comment Share on other sites More sharing options...
JTapp Posted August 5, 2013 Author Share Posted August 5, 2013 Whew.... I check out the link you gave me.. its looking over my head.. but I always play around! I'll try to figure it out.. Thanks Quote Link to comment Share on other sites More sharing options...
JTapp Posted August 5, 2013 Author Share Posted August 5, 2013 Ok I think I see what you are saying, darkfreaks. So my form offers 6 choices on how to find the data. I have only been able to find success with one choice. A second choice returns data, but it is the wrong field. Certainly this makes no sense to whoever is reading this... Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 5, 2013 Share Posted August 5, 2013 you need to form your sql query statement in a php variable and echo it so that YOU know exactly what it is. because you have hidden field(s) named metode after the <select menu using that same name, only the last one (with the value tblLodges.intLodgeNumber) is what is being searched for, regardless of what your <select> menu choice is. also, you don't need and should not prepend the table name to the column name (you know what table you are querying, you don't have to specifically list it unless you are JOINing tables in one query, and even then you should use short alias names) and to prevent sql injection you MUST validate that the column name being selected in the form is ONLY and EXACTLY one of the permitted choices. Quote Link to comment Share on other sites More sharing options...
JTapp Posted August 5, 2013 Author Share Posted August 5, 2013 Thanks mac_gyver.. I got a yippee! Then a whant whaaa.. I was reminded why I needed a 'hidden' input type So my search form looks the same but has new buttons below my main button.. I've called them 'wishthiswashidden'.. but regardless of what I do.. they are still visible. I'll keep playing (or butchering).. Thanks again. <form name="form1" method ="post" action="URL/results.php" target="_blank"> <table width="849" border="0" align="center"> <tr> <th width="812" bgcolor="#fff" class="form_1" scope="col"> <p class="style1"> <select name="metode" size="6"> <option value="strLodgeLocationCity"selected>City Lodge Is Located In</option> <option value="strLodgeName">Name of Lodge</option> <option value="intLodgeNumber">Lodge Number</option> <option value="intDistrictID">District Lodge Is In</option> <option value="strLodgeLocationZIP">Zip Code of Lodge Location</option> <option value="strLodgeCounty">Lodge County</option> </select> <span class="text_1">Search Value:</span><span class="copy_1"> <input name="search" type="text" class="form" size="25"> </span></p> <p class="heading_1"> <input type="submit" value="Click Here To Search Our Lodge Locator" name="Go"> </p> <p class="heading_1"> <input type="submit" name="metode" value="wishthiswashidden"/> <input type="Submit" name="metode" value="wishthiswashidden"/> </p> </p> Quote Link to comment Share on other sites More sharing options...
JTapp Posted August 5, 2013 Author Share Posted August 5, 2013 Scratch that last post. I said I was cross-eyed! I removed the input type lines that I don't need. Quote Link to comment Share on other sites More sharing options...
Solution JTapp Posted August 5, 2013 Author Solution Share Posted August 5, 2013 Thanks mac_gyver.. I can go to bed now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.