Problems filtering data from a form with PHP and Mysql

1

I expose my problem for which I write by this means. I'm creating a system in which the user lists some records that come from a database, in the frontend I show all the data in a table, above the table that shows the data is a form to filter the information that the user is seeing, I have seen the methods to filter according to the criteria that the user wants, apply them and it gives me as a result that I take into account some filters and others do not, the following code are the criteria that I have applied for the filter:

// filtros de busqueda
    $where = '';
    if (isset($_POST['filtrar'])) {

        //filtra remitente y destinatario
        if(!empty($_POST['r_nombre']) && !empty($_POST['d_nombre'])){

            $where=" WHERE r_nombre LIKE '%".$_POST['r_nombre']."%' OR r_apellido LIKE '%".$_POST['r_nombre']."%' OR d_nombre LIKE '%".$_POST['d_nombre']."%' OR d_apellido LIKE '%".$_POST['d_nombre']."%'  ";

        //filtra solo remitente
        }elseif(!empty($_POST['r_nombre']) && empty($_POST['d_nombre'])){

            $where=" WHERE r_nombre LIKE '%".$_POST['r_nombre']."%' OR r_apellido LIKE '%".$_POST['r_nombre']."%' ";

        //filtra solo destinatario
        }elseif(empty($_POST['r_nombre']) && !empty($_POST['d_nombre'])){

            $where=" WHERE d_nombre LIKE '%".$_POST['d_nombre']."%' OR d_apellido LIKE '%".$_POST['d_nombre']."%' ";

        //filtra desde - hasta
        }elseif(!empty($_POST['desde']) && !empty($_POST['hasta'])){

            $where="WHERE llegada_wh BETWEEN '$_POST[desde]' AND '$_POST[hasta]' ";

        //filtra por guía
        }elseif(!empty($_POST['guia'])){

            $where=" WHERE track LIKE '%".$_POST['guia']."%'";

        //filtra por status
        }elseif(!empty($_POST['estatus'])){

            $where=" WHERE status_guia = '$_POST[estatus]'";

        //filtra por tipo de cobro
        }elseif(!empty($_POST['tipo_cobro'])){

            $where=" WHERE tipo_cobro = '$_POST[tipo_cobro]'";

        //filtra el remitente por nro de guia y estatus
        }elseif(!empty($_POST['r_nombre']) && !empty($_POST['guia']) && !empty($_POST['estatus']) ){

            $where=" WHERE r_nombre LIKE '".$_POST['r_nombre']."%' OR r_apellido LIKE '".$_POST['r_nombre']."%' AND track LIKE '%".$_POST['guia']."%' AND status_guia = '$_POST[estatus]' ";

        }

    }

and here the query that brings me the data

$sql_limit = "SELECT track,llegada_wh,r_apellido,r_nombre,r_tlf,d_nombre,d_apellido,d_tlf,status_guia,peso_cobrado,total_guia,contenedores,tarifa,unidad,tipo_cobro FROM guias $where ORDER BY fecha_creacion DESC LIMIT $empezar_desde,$cant_mostrar";
    $datos_limit = $this->con->consultaRetorno($sql_limit);
    $row_limit = $datos->num_rows;

My question is: am I applying the correct method or will there be some other method of filtering the information

here are the input that receives the data

<input type=\"text\" class=\"form-control\" name=\"r_nombre\" placeholder=\"nombre o apellido\">
<input type=\"text\" class=\"form-control\" name=\"d_nombre\" placeholder=\"nombre o apellido\">
<input type=\"date\" name=\"desde\" class=\"form-control\">
<input type=\"date\" name=\"hasta\" class=\"form-control\">
<input type=\"text\" name=\"guia\" class=\"form-control\">
<select name=\"estatus\" class=\"form-control\">
                                    <option>Seleccione...</option>";
                                    while ($row_estatus = $datos_estatus->fetch_assoc()) {
                                        echo"                          
                                        <option value=\"$row_estatus[estatus]\">$row_estatus[estatus]</option>";               
                                        }                                        
                                    echo "    
                                    </select>
<select name=\"tipo_cobro\" class=\"form-control\">
                                        <option>Seleccione...</option>
                                        <option value=\"Cobro en destino\">C.O.D.</option>
                                        <option value=\"Pre-Pagado\">Pre-Pagado</option>
                                    </select>
    
asked by Diego Fajardo 16.03.2018 в 14:59
source

0 answers