select with multiple Betweens

Asked

Viewed 38 times

1

Good people,

I need a help, I’m making a filter and to make 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 it is a mistake 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 so to check if the form text box is not empty then the variable would be with the value that the user entered, but if it is empty the variable would be with a value assigned by me, which is the minimum and maximum value of each of the fields with some margin.

  • 'Between' is only for consultation between two values. To see more, use the symbols of greater (>) and smaller (<).

1 answer

2


I believe that your select Don’t be the problem, you’re using between in the right way. But the elses are not doing what I believe you intend.

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

Try to use 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";
    }  
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.