Jump to content

php search engine mysql


eddlandos

Recommended Posts

hey I'm trying to make my search engine work on the data for the input of what a user registers on the form

it gets inserted into the database the only issue I had was retrieving an Any part so when the user selects Any in the search

form it shows all cities rather than the selected city same with the sex/gender was thinking this would need lots of ifs I couldn't

find anything on the godlike google for search engines containing "Any"

heres my code so far:

search.php

<HTML>
<HEAD><TITLE>Search Registry</TITLE></HEAD>

<BODY>

<form method="POST" action="results.php">



      <th>Looking for a </th>
      <td><select name="sex">
<option>Male</option>
<option>Female</option>
<option>Couple</option>
</td>
 <tr>
      <th>City </th>
      <td>
<select name="city">
<option>Any</option>
<option>Abinger</option>
<option>Abinger Hammer</option>
<option>Addington</option>
<option>Addlestone</option>
<option>Albury</option>
<option>Aldershot</option>
<option>Alfold</option>
<option>Arundel</option>
<option>Alfold</option>
<option>Ashford</option>
<option>Ashtead</option>
<option>Badshot Lea</option>
<option>Bagshot</option>
<option>Banstead</option>
<option>Battle</option>
<option>Beddington</option>
<option>Belmont</option>
<option>Betchworth</option>
<option>Blackbrook</option>
<option>Blackheath</option>
<option>Blackwater</option>
<option>Bletchingley</option>
<option>Bletchingly</option>
<option>Bramley</option>
<option>Brook</option>
<option>Brookwood</option>
<option>Bucks Horn Oak</option>
<option>Byfleet</option>
<option>Camberley</option>
<option>Carshalton</option>
<option>Caterham</option>
<option>Caversham</option>
<option>Charlwood</option>
<option>Cheam</option>
<option>Chertsey</option>
<option>Chessington</option>
<option>Chiddingfold</option>
<option>Chipstead</option>
<option>Chobham</option>
<option>Claygate</option>
<option>Cobham</option>
<option>Coldharbour</option>
<option>Compton</option>
<option>Couldson</option>
<option>Countywide</option>
<option>Cranleigh</option>
<option>Crondall</option>
<option>Crowborough</option>
<option>Croydon</option>
<option>Crystal Palace</option>
<option>Dockenfield</option>
<option>Dorking</option>
<option>Downside</option>
<option>Dunsfold</option>
<option>East Horsley</option>
<option>Effingham</option>
<option>Egham</option>
<option>Elstead</option>
<option>Englefield Green</option>
<option>Enton Green</option>
<option>Epsom</option>
<option>Esher</option>
<option>Ewell</option>
<option>Ewhurst</option>
<option>Farleigh</option>
<option>Farnborough</option>
<option>Farnham</option>
<option>Fernhurst</option>
<option>Fetcham</option>
<option>Frimley</option>
<option>Gatwick</option>
<option>Gatwick Airport</option>
<option>Godalming</option>
<option>Godstone</option>
<option>Gomshall</option>
<option>Guildford</option>
<option>Hambledon</option>
<option>Hampton</option>
<option>Hampton Court</option>
<option>Hampton Wick</option>
<option>Hascombe</option>
<option>Haslemere</option>
<option>Hastings</option>
<option>Hersham</option>
<option>Hindhead</option>
<option>Hookwood</option>
<option>Horley</option>
<option>Horsham</option>
<option>Horsley</option>
<option>Hove</option>
<option>Hurstpierpoint</option>
<option>Kew</option>
<option>Kings Lynn</option>
<option>Kingston upon Thames</option>
<option>Kingswood</option>
<option>Knaphill</option>
<option>Leatherhead</option>
<option>Leigh</option>
<option>Limpsfield</option>
<option>Lingfield</option>
<option>Little Bookham</option>
<option>Long Ditton</option>
<option>Lower Bourne</option>
<option>Mayford</option>
<option>Mickleham</option>
<option>Milford</option>
<option>Mitcham</option>
<option>Morden</option>
<option>New Addington</option>
<option>New Malden</option>
<option>Newdigate</option>
<option>Norbury</option>
<option>Ockley</option>
<option>Ottershaw</option>
<option>Oxshott</option>
<option>Oxted</option>
<option>Peaslake</option>
<option>Petersham</option>
<option>Pirbright</option>
<option>Portslade</option>
<option>Purley</option>
<option>Puttenham</option>
<option>Redhill</option>
<option>Reigate</option>
<option>Richmond</option>
<option>Richmond-upon-Thames</option>
<option>Rye</option>
<option>Sanderstead</option>
<option>Shackleford</option>
<option>Shamley Green</option>
<option>Shepperton</option>
<option>Shere</option>
<option>South Godstone</option>
<option>Staines</option>
<option>Sunbury-On-Thames</option>
<option>Sunningdale</option>
<option>Surbiton</option>
<option>Sutton</option>
<option>Sutton Green</option>
<option>Tadworth</option>
<option>Tandridge</option>
<option>Teddington</option>
<option>Thames Ditton</option>
<option>Thornton Heath</option>
<option>Tilford</option>
<option>Twickenham</option>
<option>Virginia Water</option>
<option>Wallington</option>
<option>Walliswood</option>
<option>Walton-on-Thames</option>
<option>Walton-on-the-Hill</option>
<option>Warlingham</option>
<option>West Byfleet</option>
<option>West Clandon</option>
<option>West End</option>
<option>West Molesey</option>
<option>Westcott</option>
<option>Westhumble</option>
<option>Weybridge</option>
<option>Windlesham</option>
<option>Witley</option>
<option>Woking</option>
<option>Woldingham</option>
<option>Worcester Park</option>
<option>Worplesdon</option>
<option>Wrecclesham</option>
<option>Not Listed</option>
</td>
</tr>
<tr>


</TR>
</TABLE><BR>

<input type="SUBMIT" value="Search">
</form>

<?php


$city = $_POST['city'];
$sex = $_POST['sex'];

?>

</BODY>
</HTML>

 

and then the results.php

 

<?php
    

include 'config.php';


    $city = isset($_POST['city']) ; var_dump(mysql_real_escape_string(($_POST['city']), $conn)) ; null;
    $sex = isset($_POST['sex']) ; var_dump(mysql_real_escape_string(($_POST['sex']), $conn)) ; null;

    $sql = 'SELECT * FROM dating';

    $where = array();
    if (!empty($city))
    {
        $where[] = "city = '  $city'"; 
    }
    if (!empty($sex))
    {
        $where[] = "sex = ' $sex'"; 
    }

    if (!empty($where))
    {
        $sql .= ' WHERE ' . implode(' AND ', $where);
    }


    $result = mysql_query($sql, $conn) or die(mysql_error());

    if(mysql_num_rows($result) == 0)
    {
        echo "Sorry, there were no matches for your query. Please try again.";
    }    
    else
    {
        echo "<TABLE BORDER=1><TR><TH>First name</TH><TH>last name</TH><TH>city</TH><TH> </TH></TR>";
        while($row = mysql_fetch_array($result))
        {
            echo "<TR><TD> ". $row['city'] ." ". $row['sex'] ."</TD></TD></TR><br />";
        }
        echo "</TABLE>";
    }
    
?>

 

and the db code :)

 

CREATE TABLE `dating` (
  `member_id` int(11) unsigned NOT NULL auto_increment,
  `body` varchar(100) default NULL,
  `hair` varchar(100) default NULL,
  `race` varchar(100) default NULL,
  `gender` varchar(100) default NULL,
  `sex` varchar(100) default NULL,
  `city` varchar(100) default NULL,
  `age` varchar(100) default NULL,
  `firstname` varchar(100) default NULL,
  `lastname` varchar(100) default NULL,
  `login` varchar(100) NOT NULL default '',
  `passwd` varchar(32) NOT NULL default '',
  PRIMARY KEY  (`member_id`)
) TYPE=MyISAM;

Link to comment
Share on other sites

Do you mean select all the cities? You will need to change your select drop down box, all put unique value with it, so for an example,

<option value="any">Any</option>

Then do a if statement,

if ($_POST['city'] == 'any') {
// mysql_query("SELECT * FROM cities");
}

 

Hope this is what you're looking for.

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.