select with multiple BETWEENs

1

Good people,

I need a help, I'm doing a filter and to do the select I did so:

$molcomp_query="SELECT * FROM stock_comp WHERE (diametroaco BETWEEN '$d1' and '$d2')
AND (comprimentototal BETWEEN '$comp1' AND '$comp2') 
AND (diametroexterior BETWEEN '$de1' AND '$de2') $ordenar_por"; 

but you are giving error in this area:

if(isset($_POST)&&!empty($_POST)){ 
    if ($d1<>""){
        $d1=$_POST["d1"];
    }else{
        $d1=="0,200";
    }

    if ($d2<>""){
        $d1=$_POST["d2"];
    }else{
        $d2=="20";
    }

    if ($comp1<>""){
        $d1=$_POST["comp1"];
    }else{
        $comp1=="1";
    } 

    if ($comp2<>""){
        $d1=$_POST["comp2"];
    }else{
        $comp2=="10000";
    } 

    if ($de1<>""){
        $d1=$_POST["de1"];
    }else{
        $de1=="1";
    }

    if ($de2<>""){
        $d1=$_POST["de2"];
    }else{
        $de2=="200";
    }  
}

I did this to check if the form's text box is not empty so the variable would have the value that the user entered, but if it is empty the variable would have a value assigned by me, which is the minimum, and the maximum value of each field with some margin.

    
asked by anonymous 10.05.2018 / 10:35

1 answer

2

I believe your select is not the problem, you're using between the right way. But the elses are not doing what I believe you want.

For example, the first one is thus $d1=="0,200"; , comparing instead of assigning; this should be reflecting in the query.

Try using it like this:

if(isset($_POST)&&!empty($_POST)){ 
    if ($d1<>""){
        $d1=$_POST["d1"];
    }else{
        $d1="0,200";
    }

    if ($d2<>""){
        $d1=$_POST["d2"];
    }else{
        $d2="20";
    }

    if ($comp1<>""){
        $d1=$_POST["comp1"];
    }else{
        $comp1="1";
    } 

    if ($comp2<>""){
        $d1=$_POST["comp2"];
    }else{
        $comp2="10000";
    } 

    if ($de1<>""){
        $d1=$_POST["de1"];
    }else{
        $de1="1";
    }

    if ($de2<>""){
        $d1=$_POST["de2"];
    }else{
        $de2="200";
    }  
}
    
10.05.2018 / 12:22