...

View Full Version : Searching mysql through a form



graham23s
11-18-2007, 01:45 PM
Hi Guys,

i have 5 basic fields in a form, when submitted search mysql to give back results , but i'm having trouble with 1 of the fields its the search between specific ages for example: 20 and 50 give all the results back of the users between these ages

code:


<?php
## deal with the submission
if(isset($_GET['search'])) {

echo ("<hr />");

## vars
$gender = $_GET['gender'];
$herefor = $_GET['herefor'];
$country = $_GET['from'];
$frage = $_GET['fromage'];
$to = $_GET['toage'];

//echo $gender.$herefor.$from.$frage.$to;

## compile the query
$searchquery = "SELECT * FROM `users` ";
$searchquery .= "WHERE `gender`='" . $gender . "' ";
$searchquery .= "AND `herefor`='$herefor' ";
$searchquery .= "AND `country`='$country'";
$searchquery .= " AND `year` BETWEEN YEAR(CURDATE() - INTERVAL $toage YEAR) AND YEAR(CURDATE() - INTERVAL $fromage YEAR)";

## run query
$resultquery = mysql_query($searchquery);

## num results
$numresults = mysql_num_rows($resultquery);

echo $numresults;

}

?>

and input would be appreciated

Graham

fl00d
11-18-2007, 04:32 PM
It looks to me like you've used incorrect variables.
You've set $frage to equal $_GET['fromage'], but in the query you use a variable called $fromage. Were you intending to do that and $fromage has been set somewhere else that isn't shown in the current portion of the script? Same go's for $to and $toage.


$searchquery .= " AND `year` BETWEEN YEAR(CURDATE() - INTERVAL $to YEAR) AND YEAR(CURDATE() - INTERVAL $frage YEAR)";

graham23s
11-18-2007, 07:43 PM
LOL damnn yep that was it, i was using other variables further up the script

thanks for that mate

Graham



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum