Checking if registration already exists in the database

Asked

Viewed 2,101 times

-1

I am using this function in my form but I have a problem.

It is validating the field and is not included in the comic, but the message instead of saying that the client already exists is saying that it has been successfully registered.

how to fix this?... if it already exists in the comic book it informs that the client already exists and if it does not exist it informs that it has been registered?

my Cód is like this:

$search = mysql_query("SELECT * FROM clientes WHERE cnpj = '$cnpj'");
if(@mysql_num_rows($search) > 0){
echo 'Esse Cliente já existe';
}else
// faz inserção

     mysql_query("INSERT INTO clientes (dia,mes,ano,data2,nome,fantasia,contato,fone,celular,email,cnpj,estadual,endereco,cep,bairro,cidade,estado,nf,desconto,obs) values
                                       ('$dia','$mes','$ano','$data2','$nome','$fantasia','$contato','$fone','$celular','$email','$cnpj','$estadual','$endereco','$cep','$bairro','$cidade','$estado','$nf','$desconto','$obs')");
   }
}

echo '<script type="text/javascript">
             alert("Registro Incluído com Sucesso!!!"); 
    </script>';

//Atualize a página
echo
'<script type="text/javascript">location.replace("clientes.php");</script>';

2 answers

0


$search = mysql_query("SELECT * FROM clientes WHERE cnpj = '$cnpj'");
if(@mysql_num_rows($search) > 0){
    echo '
        <script type="text/javascript">
             alert("Esse Cliente já existe"); 
        </script>
    ';
}else{

    // faz inserção

     mysql_query("INSERT INTO clientes (dia,mes,ano,data2,nome,fantasia,contato,fone,celular,email,cnpj,estadual,endereco,cep,bairro,cidade,estado,nf,desconto,obs) values('$dia','$mes','$ano','$data2','$nome','$fantasia','$contato','$fone','$celular','$email','$cnpj','$estadual','$endereco','$cep','$bairro','$cidade','$estado','$nf','$desconto','$obs')");

    echo '
        <script type="text/javascript">
            alert("Registro Incluído com Sucesso!!!"); 
        </script>
    ';
}

//Atualize a página
echo '<script type="text/javascript">location.replace("clientes.php");</script>';
  • Thanks was that, now it worked correctly..... thank you very much

0

The section where informs the success of the inclusion must be within the Else block. In the question code this excerpt is outside of Else, so it always shows the message.

Browser other questions tagged

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