Search from a form

Asked

Viewed 33 times

0

I have a form with all the districts of portugal. When I select a city I do the search through the method post, get the value city and research shows everything that exists with the city countryside. So far so good. But when the value city is equal to ALL or (all), as I can say in the query that the user wants to see all cities?

Receiving the values of the form:

$localidade=$_REQUEST['localidade'];
$genero=$_REQUEST['genero'];
$idade_1=$_REQUEST['age-min'];
$idade_2=$_REQUEST['age-max'];

Consultation:

$sql = "SELECT  u.id, u.username, u.genero, u.idade, u.local, u.descricao,   u.status,u.last_login, u.photo_p_id, p.location 

FROM user AS u 

LEFT JOIN photos AS p

ON u.photo_p_id=p.id 
WHERE local='$localidade' AND genero ='$genero' AND idade BETWEEN '$idade_1' and '$idade_2'


LIMIT 5;
";

What do you think of my solution?

Receiving the form values:

$consulta="local='$localidade' AND";
}else{$consulta="";}
$genero=$_REQUEST['genero'];
$idade_1=$_REQUEST['age-min'];
$idade_2=$_REQUEST['age-max'];

Consultation:

$sql = "SELECT  u.id, u.username, u.genero, u.idade, u.local, u.descricao,  u.status,u.last_login, u.photo_p_id, p.location 

FROM user AS u 

LEFT JOIN photos AS p

ON u.photo_p_id=p.id 
WHERE $consulta genero ='$genero' AND idade BETWEEN '$idade_1' and '$idade_2'


LIMIT 5;
";
  • 1

    Delete the part local='$localidade' AND of the WHERE clause

  • 1

    Or create 2 querys, one for when filtering cities and another to bring all.

  • what do you think of the solution I’ve found?

  • If it works, it looks like OK!. I don’t know anything about php! :(

  • In this code, the first block, is where I take the values of input form fields. In the second block is where I query the database according to the values I picked from the form

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.