-1
I have the following table, called 'animal'':
And in my PHP application using PDO, I need to select values that are conditional. For example:
The user will pass as a parameter only the 'TYPE' and 'Status' fields (which are required). And from there it would be returned to him all the animals registered with the entered values. Until there everything right.
My problem starts when the user decides to insert parameters other than the mandatory ones, because I need to generate new querys with the inserted fields. I would like to know how to do this without having to create a new query for each parameter inserted, preferably.
My code is currently like this:
if(isset($_GET['status']) && isset($_GET['pet'])){
$status = $_GET['status'];
$especie = $_GET['pet'];
$query = "SELECT * FROM animal WHERE ( TIPO = :TIPO , Status = :STATUS)";
$stmt = $conecta->prepare($query);
$stmt->bindParam(':TIPO',$especie);
$stmt->bindParam(':STATUS',$status);
$stmt->execute();
}
It works perfectly, but I can’t develop the logic for my problem!
Other parameters sent via GET by the search page:
$_GET['raca-dog']
$_GET['raca-cat']
$_GET['tamanho']
$_GET['cor']
$_GET['informacao']
$_GET['local']
$_GET['petPic']
What I once thought:
- Create an array that, if the field is set in $_GET, adds the value to the end of the array and then passes the entire array as paramtro pro select (I think it’s gambiarra)
- Create a sequence of conditions that check the fields set and their values, and for each field set create a new query adding the field value (More)
Image of the search screen:
recalling that only the SITUATION and the SPECIES are mandatory.
What syntax is this for the condition of its WHERE clause (WHERE ( TYPE = :TYPE , Status = :STATUS))? What does this mean ,?
– anonimo
is a way to pass a parameter without passing the value directly, and can assign the value you want to this parameter, as I did in: $stmt->bindParam(':TYPE',$specify); $stmt->bindParam(':STATUS',$status);
– Vitor Couto
The search parameter must be selected by the user or he can enter something in the search field and return the tables where any of the fields are equal to the search?
– adrianosmateus
It may or may not insert the data in the other fields, I will update the question with a screenshot of the search screen
– Vitor Couto
ready, updated to better understand
– Vitor Couto
What I asked is that, according to my knowledge, there is no such expression in the syntax defined for SQL command. Maybe you want to use a logical operator.
– anonimo
is PHP the syntax
– Vitor Couto
But you are sending the string, with an SQL command, to run in your DBMS, has nothing to do with PHP.
– anonimo