Check if value is between two column values

I am new to php and sql, so I am having problems with the task.

Task: I have a db with 3 columns having the city name, minValue and maxValue. This db should have about 200 lines, for example:

| COL 1 | COL 2 | COL 3 |

| Sao paulo | 65000000 | 65050000 |

| Brasilia | 69,000,000 | 65090000 |

I have to get the value and check if it is between the values ​​from COL 2 and COL 3, and this should be checked for each row in the table.

Now I get the value from the message and do the following:

 <body>
  <?php 
  $cep = $_POST["cep"];
  $connect = mysqli_connect("--db settings--");
  $query = mysqli_query($connect, "SELECT * FROM tabela_cep WHERE $cep BETWEEN `COL 2` AND `COL 3`");
  $dados= mysqli_fetch_array($query);

    $cep_lenght = strlen((string)$cep);
    if($cep_lenght == 7) {
        $cep = (string)$cep;
        $cep = "0" . $cep;
    }else{
        return;
    }
    if($dados) {
    echo "<p>The value <strong>$cep</strong> is valid !</p>";
  }else {
    echo "<p>The value <strong>$cep</strong> is invalid !</p>";
  }
     ?>

The problem is this: every value I pass is considered valid.

PHP , , , .. ?

+4
1
SELECT * FROM tabela_cep WHERE $cep > `COL 2` AND $cep < `COL 3`

, , . , , SQL.

0

All Articles