Jump to content

Search using $metode.. can't trim.. help please


JTapp
Go to solution Solved by JTapp,

Recommended Posts

 

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');
    }?>
        
 
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

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... :-\  

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.