1
I need to do a PHP search in which the user selects certain filters and get existing results from them in the database. The problem is when I open the page itself, as the variable initializes empty causing Undefined index error. The question is, how can I check before the page clicks 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>    
<span> <?php echo $row['estado']; ?> </span>
</div>
</li>
</ul>
<?php
}
}
?>
I tried to put a ! isset but it didn’t work out and he just stopped researching, so I don’t know what else to do. I appreciate any help
Edit1: I switched the issets for Empty but it still didn’t work. It turns out that the error is repeated:
<?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 only had to change the order of the code. I passed the block that defined the variables to after my isset. Now it works right
Okay, I put Empty in to see if it works, and he does the right thing. I found that the problem is in relation to logic, but I have no idea how to solve it
– Bryan de Almeida