I'm doing a message page in PHP and MySQL, I have a table called usuarios
with the fields id
, user
, edad
, pais
and ciudad
.
I would like the user to be able to search for other users according to certain parameters, such as age; For this I have the following form:
<form action="search.php" method="post">
<label>Search By: </label> <br>
<input type="text" value="" name="usernam" placeholder="Username"><br>
<input type="number" value="" name="age" placeholder="Age" min="10" max="150"><br>
<input type="text" value="" name="country" placeholder="country"><br>
<input type="text" value="" name="city" placeholder="city"> <br><br>
<input id="" type="submit" value="Search" name="search"></form>
And then the following PHP code:
if(isset($_POST['search'])) {
$usernam = $_POST['usernam'];
$age = $_POST['age'];
$country = $_POST['country'];
$city = $_POST['city'];
}
I still have to do the SQL query to get the data, but I do not know how I could get that if the user does not enter all parameters (for example, enter only the country and city fields) the query ignores the fields that are blank, which in this example would be usuario
and edad
.
How could I make this query?