PHP search with combobox

1

I need to do a PHP search where the user selects certain filters and obtains results from the database. The problem is when I open the page itself because the variable is initialized empty causing undefined index error. The question is, how can I do a check before the page loads to give a valid value to the variable?

Code:

<form action ="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST" class="w3-container">
    <p>
    <label> Eixo Musical: </label>
    <select name="opt_eixo" class="w3-select">
        <?php
        $getUsuario = "SELECT DISTINCT tipo_usuario FROM tb_usuario";
        $getUsuarioQuery = mysql_query($getUsuario) or die(mysql_error());

        while($getUsuarioLinha = mysql_fetch_array($getUsuarioQuery)){
        $Usuario = $getUsuarioLinha['tipo_usuario'];
        $idUsuario = $getUsuarioLinha['cod_usuario'];
        echo "<option name='$Usuario' value='$Usuario'>$Usuario</option>";
        }
        ?>
    </select>

    <br><br>

    <label> Gênero musical: </label>
    <select name="opt_genero" class="w3-select">
      <?php
      $getGenero = "SELECT * FROM tb_genero ";
      $getGeneroQuery = mysql_query($getGenero) or die(mysql_error());

      while($getGeneroLinha = mysql_fetch_array($getGeneroQuery)){
        $Genero = $getGeneroLinha['genero_1'];
        $Genero_id = $getGeneroLinha['cod_interesse'];
        echo "<option name='$Genero' value='$Genero'>$Genero</option>";
      }
      ?>
    </select>

    <br><br>

    <label> Estado: </label>
    <select name="opt_estado" class="w3-select">
      <?php
      $getEstado = "SELECT * FROM tb_usuario";
      $getEstadoQuery = mysql_query($getEstado) or die(mysql_error());

      while($getEstadoLinha = mysql_fetch_array($getEstadoQuery)){
        $Estado = $getEstadoLinha['estado'];
        $idEstado = $getEstadoLinha['cod_usuario'];
        echo "<option name='$Estado' value='$Estado'>$Estado</option>";
      }
      ?>
    </select>
    <br><br>
    <input type="submit" class="w3-button w3-deep-purple" value="Pesquisar" />
  </form>
</div>

<div class="w3-container" style="padding-left: 25%">
  <?php 
      $eixo = $_POST["opt_eixo"];
      $genero = $_POST["opt_genero"];
      $estado = $_POST["opt_estado"];
      //Eu acredito que o erro esteja por aqui

  $sql = "SELECT * FROM tb_genero INNER JOIN (tb_usuario INNER JOIN  tb_interesse ON tb_usuario.cod_usuario = tb_interesse.cod_usuario) ON tb_genero.cod_interesse = tb_interesse.cod_interesse WHERE tb_genero.genero_1 LIKE '$genero' AND tb_usuario.estado LIKE '$estado' AND tb_usuario.tipo_usuario LIKE '$eixo' ";

  $res = SQLExecute($con,$sql);

  $quant = (mysql_num_rows($res));

if ($quant==0){
  ?>
  <div class='w3-container'>
    <center><h2>bem vindo(a) a página de pesquisas. </h2></center>
  </div>
  <?php
}

else {
?>

  <div class="w3-container">
  <center><h2>Resultados</h2></center>

  <?php
    while ($row=mysql_fetch_array($res)){
  ?>

  <ul class="w3-ul w3-card-4">
    <li class="w3-bar">
      <span onclick="this.parentElement.style.display='none'" class="w3-bar-item w3-button w3-white w3-xlarge w3-right">×</span>
      <img src="/o php modificado/avatar.jpg" class="w3-bar-item w3-circle w3-hide-small" style="width:85px">
      <div class="w3-bar-item">
      <span class="w3-large"> <?php echo $row['nome_usuario']; ?> </span><br>
      <span> <?php echo $row['tipo_usuario']; ?> </span> &nbsp&nbsp&nbsp 
      <span> <?php echo $row['estado']; ?> </span>
      </div>
    </li>
  </ul>
<?php
    }
}
?>

I tried to put a! isset but it did not work and it just stopped searching, so I do not know what else to do. Thanks any help

Edit1: I changed the issets to empty but continued without success. It turns out that the error repeats itself:

<?php 
$emptyVar = false; //variável de validação
    if (empty($eixo)) {
      $eixo = $_POST["opt_eixo"];
    }
      else if (empty($eixo) ) {
        # code...
        $eixo = $_POST["opt_eixo"];
        $emptyVar = false;
      }

    if (empty($genero)) 
    {
      $genero = $_POST["opt_genero"];
    }
      else if (empty($genero)) {
        # code...
        $genero = $_POST["opt_genero"];
        $emptyVar = false;
      }

    if (empty($estado)) 
    {
      $estado = $_POST["opt_estado"];
    } 
      else if (empty($estado)) {
        # code...
        $estado = $_POST["opt_estado"];
        $emptyVar = false;
      }


     if (!isset($_POST["Pesquisar"])) { 
//esse Pesquisar é o nome/id do botão de submit
       # code...
       echo "<div class='w3-container'>
               <center><h2> bem vindo(a) a página de pesquisas. </h2></center>
             </div>";

     } else {

      //$sql = "SELECT * FROM tb_usuario WHERE estado LIKE '$estado' AND tipo_usuario LIKE '$eixo'";
      $sql = "SELECT * FROM tb_usuario INNER JOIN tb_genero ON tb_usuario.cod_usuario = tb_genero.cod_usuario WHERE tb_genero.genero_1 LIKE '%$genero%' AND tb_usuario.tipo_usuario LIKE '$eixo' AND tb_usuario.estado LIKE '$estado'";

      $res = SQLExecute($con,$sql);

      $quant = (mysql_num_rows($res));

      if ($quant==0){
        echo "<div class='w3-container'><center><h2> bem vindo(a) a página de pesquisas. </h2></center></div>";
      } else {
  ?>

Edit2: I just needed to switch the order of the code. I passed the block that defined the variables for after my isset. Now it works right

    
asked by anonymous 07.11.2018 / 17:40

1 answer

1

The isset should work for you. In the meantime try to use the empty:

empty($variavel)

See here the return of [Sougata Bose] on this type of chacagem.

If necessary, compare your variable directly:

if ($variavel !== null)
    
07.11.2018 / 17:59