Problems in PHP [closed]

1

Hi everyone! I'm new to PHP and MySQL Database.

For several days I've been breaking my head with a code and I can not find the error at all. :

I have an index.php page, with a form calling the file "cep.php". In this file, I need the command to read the input sent by the user, and check if the data entered does not exist in the "cep" table, in the "CEP" field.

If the zip exists in bd, it redirects to the page "ok.php" If it does not exist, it redirects to the "bad.php" page.

index.php:

<form method="post" action="cep.php" >
   <input type="text" class="form-control form-white" placeholder="Digite seu CEP" name="cepinput" id="cepinput" maxlength="9" OnKeyPress="formatar('#####-###', this)" />
   <input type="submit" class="btn btn-submit" value="Verificar Disponibilidade" />
</form>

cep.php:

<?php

require "conexao.php";

// Recuperamos o cep enviado pelo formulário
$cep = $_GET['cepinput'];

// Verificamos no banco de dados o cep equivalente
$resultado = mysql_query("SELECT * FROM cep WHERE CEP=$cep");

// Descobrimos o total de registros encontrados
$num_rows = mysql_num_rows($resultado);

// Se houver o cep informado
if ($num_rows > 0) {

// Exibe a página OK
$url = 'ok.php';
echo'<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';  

// Se não houver o cep informado
} 
else {
$urlerro = 'bad.php';
echo'<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$urlerro.'">';  
}

?>

The error happens when I hit the "submit" button. It returns the page "bad.php" even though I insert an existing zip in the DB.

Thanks in advance! ;)

    
asked by anonymous 22.03.2016 / 23:44

1 answer

0

Try PDO is safer, use this algorithm here and see if you like it, it's tested and it works.

<?php try { $conect = new PDO("mysql:host=" . $mysql_host . "; dbname=" . $mysql_base, $mysql_user, $mysql_pass);
    $conect -> setAttribute(PDO :: ATTR_ERRMODE, PDO :: ERRMODE_EXCEPTION);
    $conect -> exec("set names utf8");
    $statement = $conect -> prepare("SELECT * FROM cep WHERE CEP = :cep");
    $statement_array = array(':cep' => $_GET["cepinput"]);
    foreach($statement_array as $array_key => $bindvalue) {
        $statement -> bindValue($array_key, $bindvalue); }
    $statement -> execute();
    $statement = $statement -> fetchAll();
    if(count($statement)) {
        $url = 'ok.php';
        echo'<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
    } else {
        $urlerro = 'bad.php';
        echo'<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$urlerro.'">';
    }
$conect = null; } catch(PDOException $error) { echo $error -> getMessage(); }
    
23.03.2016 / 12:07