Jump to content

PHP/MySQL search


PHPnewby!

Recommended Posts

Hi guys,

 

I'm trying to allow for the search of gender, interests, DOB, travelaspirations and message within my database and have come up with the following code as a start...but I'm officially stuck! Any ideas? I'd also like to encorporate DOB to be searched between DOB_from and DOB_to to search a range of birth years...any ideas? Thanks!!

 

                    <form name="search" method="post" action="<?=$PHP_SELF?>">

                      <table width="100%" border="0">

                        <tr>

                          <td width="26%">Gender</td>

                          <td width="27%"><select name="Gender" id="Gender">

                            <option>Female</option>

                            <option>Male</option>

                          </select>

                          </td>

                          <td width="21%"> </td>

                          <td width="26%"> </td>

                        </tr>

                        <tr>

                          <td>Interests</td>

                          <td><input name="Interests" type="text" id="Interests" /></td>

                          <td> </td>

                          <td> </td>

                        </tr>

                        <tr>

                          <td>Year of Birth from:</td>

                          <td><input name="DOB_from" type="text" id="DOB_from" /></td>

                          <td>Year of Birth to: </td>

                          <td><input name="DOB_to" type="text" id="DOB_to" /></td>

                        </tr>

                        <tr>

                          <td>Travel Aspirations</td>

                          <td><input name="TravelAspirations" type="text" id="TravelAspirations" /></td>

                          <td> </td>

                          <td> </td>

                        </tr>

                        <tr>

                          <td>Message </td>

                          <td><input name="Message" type="text" id="Message" /></td>

                          <td> </td>

                          <td> </td>

                        </tr>

                      </table>

                      <p>

                        <input type="hidden" name="searching" value="yes" />

                        <input type="submit" name="search" value="Search" />

                                            </p>

                    </form>

<?php

 

//This is only displayed if they have submitted the form

if ($searching =="yes")

{

echo "<h2>Members who met your search criteria</h2><p>";

 

//If they did not enter a search term we give them an error

if ($find == "")

{

echo "<p>You forgot to enter a search term";

exit;

}

 

// Otherwise we connect to our Database

require_once('Connections/TCO.php');

 

 

// We perform a bit of filtering

$find = strtoupper($find);

$find = strip_tags($find);

$find = trim ($find);

 

//Now we search for our search term, in the field the user specified

$data = mysql_query("SELECT * FROM Members WHERE (Gender = '%Gender') OR (Interests LIKE '%Interests%') OR (DOB = '%DOB_from') OR  (TravelApsirations LIKE '%TravelAspirations%') OR (Message LIKE '%Message%')");

 

//And we display the results

while($result = mysql_fetch_array( $data ))

{

echo $result['Gender'];

echo "<br>";

echo $result['DOB'];

echo "<br>";

echo $result['Interests'];

echo "<br>";

echo $result['Interests'];

echo "<br>";

echo $result['TravelAspirations'];

echo "<br>";

echo $result['Message'];

echo "<br>";

echo "<br>";

}

 

//This counts the number or results - and if there wasn't any it gives them a little message explaining that

$anymatches=mysql_num_rows($data);

if ($anymatches == 0)

{

echo "Sorry, but we can not find an entry to match your query<br><br>";

}

 

//And we remind them what they searched for

echo "<b>Searched For:</b> " .$find;

}

?>

Link to comment
https://forums.phpfreaks.com/topic/38204-phpmysql-search/
Share on other sites

well looks like you are not searching for any of your variables. You are just searching for the variable name, because you forgot your "$" infront of you search words.

 

should be

$Gender = $_POST['Gender'];
$Interests = $_POST['Interests'];
$DOB_from = date("Y-m-d", strtotime($_POST['DOB_from']));
$DOB_to = date("Y-m-d", strtotime($_POST['DOB_to']));
$TravelAspirations = $_POST['TravelAspirations'];
$Message = addslashes($_POST['Message']);

$data = mysql_query("SELECT * FROM Members WHERE (Gender = '%$Gender') OR (Interests LIKE '%$Interests%') OR (DOB BETWEEN '$DOB_from' AND '$DOB_to') OR  (TravelApsirations LIKE '%$TravelAspirations%') OR (Message LIKE '%$Message%')");

 

Ray

Link to comment
https://forums.phpfreaks.com/topic/38204-phpmysql-search/#findComment-182942
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.