Problems in PHP

Asked

Viewed 56 times

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

  • 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.

  • 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");

  • No missing simple quotes in sql? SELECT * FROM cep WHERE CEP='$cep' ?

  • 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!!

  • Why instead of using meta refresh you do not choose to use the command header('location:destino.php'); to make the redirect?

  • I did it, Adriano! Thanks for the tip! ;)

Show 2 more comments

1 answer

0

Try with PDO is safer, use this algorithm here and see if like, is tested and 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(); }
  • Thanks for the security tip, buddy! In case, I have a separate.php related file. Insert the first 3 lines into it and the rest into my cep.php? Thank you!

  • you just need to put the database, user and password, the rest it does for you, do not need the connected.php file because it will return in Count whether or not the typed zip only try to avoid * in select i.e., from SELECT * FROM cep WHERE CEP = :cep put SELECT CEP FROM cep WHERE CEP = :cep, replace the variables with their values or place them inside the.php connection

  • Cool!! I’ll try to use it! ;) Brawl!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.