1
Hey, you guys, come on! I’m new with PHP and Mysql Database.
For several days I’ve been breaking my head with a code and I can’t 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 in exists in the table "cep", in the field "CEP".
If the zip code exists in the bd, redirect to the "ok.php" page 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 press the "Submit" button. It returns the "bad.php" page, even if I insert an existing zip code into the BD.
From now on, thank you very much! ;)
What error are you making? Note that you do not need this $url or $urlerro variable
– Renato Tavares
Hello, Renato! Thank you for your reply. So the error is that when I press the "Submit" button, it returns the "bad.php" page, even if I insert an existing zip code in the BD. As for not needing the variable, on which line do you say? Grateful.
– Vinícius Lisboa
Put a valid zip code and see if mysql_num_rows is returning the actual amount, if you are trying to redirect thus header("Location: ok.php");
– Paul Polidoro
No missing simple quotes in sql?
SELECT * FROM cep WHERE CEP='$cep'
?– rray
Really, it lacked simple quotes! I had no attempt to do so. Better do with header even, Paul! Thanks a lot for the help, guys!!
– Vinícius Lisboa
Why instead of using
meta refresh
you do not choose to use the commandheader('location:destino.php');
to make the redirect?– Adriano Luz
I did it, Adriano! Thanks for the tip! ;)
– Vinícius Lisboa