WHERE query conditioned by search parameters

3

I want to make a query where I show a result by selecting a city or by selecting a month or search by id_nodo or search by city and month to show me all the nodes in that city that month, I attach an image of my form so that you can understand me better.

SELECT * FROM huawei_gestion_proyectos 
WHERE (CIUDAD ='$CIUDAD' 
    OR MES_DESTINO = '$MES_DESTINO' 
    OR ID_NODO = '$ID_NODO') 
    OR (CIUDAD ='$CIUDAD' 
    AND MES_DESTINO = '$MES_DESTINO')
    
asked by Anderviver 28.07.2017 в 23:11
source

1 answer

5

You can create a base query and concatenate the condition ( WHERE ) based on the values entered by the user in the form.

$consulta = "SELECT * FROM huawei_gestion_proyectos ";

if (isset($ciudad, $mes_destino) && $ciudad != "" && $mes_destino != "") {
    $consulta .= "WHERE ciudad = ? AND mes_destino = ?";
} elseif (isset($ciudad) && $ciudad != "") {
    $consulta .= "WHERE ciudad = ?";
} elseif (isset($mes_destino) && $mes_destino != "") {
    $consulta .= "WHERE mes_destino = ?";
} elseif (isset($id_nodo) && $id_nodo != "") {
    $consulta .= "WHERE id_nodo = ?";
} else {
    $mensaje = "Debes elegir una ciudad, un mes de destino o escribir el id del nodo";
}
    
answered by 29.07.2017 / 01:54
source