Search PHP / MySQL (show only result exactly the same as typed)

0

php collects input data using POST method. He is connected right, he is searching, but when typing the UF and Municipality he does not only show me exactly what I typed, but also the related ! I did not know what to do, but I did not know what to do. anyway, show everyone of them, or ã and I want to show ONLY EXACTLY WHAT I DIGIED. If anyone knows and can help, I am very grateful. Here is my link:

<?php

include ('conectar.php');

$connection = mysqli_connect($host, $user, $pass,$database);
 if (!$connection) { echo ("Erro ao conectar"); }

$opcao = $_POST["opcao"];
if($opcao == "2"){
    $sg_uf = '"%'. $_POST["txtsg_uf"] .'%"';
    $nm_municipio = '"%'. $_POST["txtnm_municipio"] .'%"';
    $nm_candidato = '"%'. $_POST["txtnm_candidato"] .'%"';
    $dt_ano = '"%'. $_POST["txtdt_ano"] .'%"';

$query = "SELECT * FROM resultado "
. "WHERE sg_uf LIKE ".$sg_uf." OR nm_municipio LIKE ".$nm_municipio." OR nm_candidato LIKE ".$nm_candidato." OR dt_ano LIKE ".$dt_ano."";

    $result =  mysqli_query($connection,$query);
    if (!$result) {
      die("Query inv&aacute;lida na sele&ccedil;&atilde;o dos dados");
    }

    while ($dados=  mysqli_fetch_array($result,MYSQLI_ASSOC)) {

        if ($result > 0){
            echo "Op&ccedil;&atilde;o de Busca Avan&ccedil;ada/Elei&ccedil;&otilde;es Municipais: <br />";
            echo "Encontramos!<br/ >";
            echo "UF do Estado: $dados[sg_uf]<br />"; 
            echo "Nome do Munic&iacute;pio: $dados[nm_municipio]<br />"; 
            echo "Nome do Candidato: $dados[nm_candidato]<br />"; 
            echo "Ano da Elei&ccedil;&atilde;o: $dados[dt_ano]<br />"; 
            echo "<hr>";
        }
        else {
            echo "Desculpe, nada foi encontrado.";
        }
    }
}

?>
    
asked by anonymous 30.03.2016 / 02:09

2 answers

2

Well, thank you all for helping me, especially Bacco, thank you very much! From what I've been through here I came up with the following result:

<?php
include ('conectar.php');


$sg_uf = '"%'. $_POST["txtsg_uf"] .'%"';
$nm_municipio = '"%'. $_POST["txtnm_municipio"] .'%"';
$nm_candidato = '"%'. $_POST["txtnm_candidato"] .'%"';
$dt_ano = '"%'. $_POST["txtdt_ano"] .'%"';

$connection = mysqli_connect($host, $user, $pass,$database);
 if (!$connection) {
 echo ("Servidor temporariamente fora de servi?o"); 
}

$query = "SELECT * FROM resultado "
. "WHERE sg_uf LIKE ".$sg_uf." and  nm_municipio LIKE ".$nm_municipio." and  nm_candidato LIKE ".$nm_candidato." and  dt_ano LIKE ".$dt_ano."";
$result =  mysqli_query($connection,$query);
if (!$result) {
  die("Query invalida");
}

while ($dados=  mysqli_fetch_array($result,MYSQLI_ASSOC)) {

if ($result > 0){

echo " Encontramos!<br />";

    echo "UF do Estado: $dados[sg_uf]<br />"; 
    echo "Nome do Municipio: $dados[nm_municipio]<br />"; 
    echo "Nome do Candidato: $dados[nm_candidato]<br />";
    echo "Ano das Eleicoes: $dados[dt_ano]<br />";  
    echo "<hr>";

}
else {
    echo "Desculpe, nada foi encontrado";
}
}
?>

That's it, it's working fine, it just shows me exactly what I typed.

    
30.03.2016 / 15:20
1

Sarah, try changing your SQL to the following way:

    $query = "SELECT * FROM resultado "
. "WHERE sg_uf LIKE '%".$sg_uf."' OR nm_municipio LIKE '%".$nm_municipio." OR      nm_candidato LIKE ".$nm_candidato." OR dt_ano LIKE ".$dt_ano."";

What I did was put a single quote after LIKE with the wildcard%.

    
30.03.2016 / 02:25