Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Antes de cadastrar um Cliente preciso verificar se aquele CNPJ que o usuario vai digitar para cadastra-lo já possui no banco de dados, onde CNPJ = $objCliente->CNPJ == sql - Tabela: tb_cliente -> de_cnpj varchar(20) etc... se estiver cadastrado mostrar um alert, caso o contrario conclua o cadastro.
Uso o PhpMyAdmin como gerenciador de banco de dados.
Bruno, o sistema está composto por Classes = Cliente.class.php - está é a classe Cliente onde insere bancos, tem functions etc...
tem o cliente-cadastrar.php (Parte "HTML", "JAVA" etc.. a parte da interface vamos se dizer.. ) e cliente-cadastrar-exec.php (Parte das execuções do cliente cadastrar). obs: só para ficar um pouco mais por dentro do assunto.
Eu tentei da seguinte maneira mas não funcionou... não me deu nenhum error mas nenhum também :
cliente-cadastrar.php:
<div class="form-group">
<label class="col-sm-2 control-label" for="cnpj">CNPJ</label>
<div class="col-sm-4">
<input type="text" name="cnpj" class="form-control" id="cnpj" onChange="verificar_duplicidade(this.value)" value="<?php echo str_pad($objCliente->cnpj, 14, "0", STR_PAD_LEFT); ?>" placeholder="CNPJ">
</div>
<script>
function verificar_duplicidade (cnpj)
{
if (<?php $sql = "SELECT de_cnpj FROM tb_cliente GROUP BY de_cnpj HAVING COUNT(de_cnpj) > 1"; ?> ) {
alert ("CNPJ JÁ EXISTENTE");
}
else {
alert ("CADASTRADO COM SUCESSO ! ");
}
OUTRAS FUNÇÕES.....
</script>
rafinha_sam uma dica
use a função code [<>] do fórum pra formatar os códigos, fica muito mais fácil pra quem vai ler entender, lá tu escolhe o tipo de código PHP ou HTML ou o que for
>
7 minutos atrás, marsolim disse:
rafinha_sam uma dica
use a função code [<>] do fórum na hora de postar código, fica muito fácil pra quem vai ler entender, lá tu escolhe o tipo de código PHP ou HTML ou o que for
Obrigado, já foi alterado !
Não cara.. esqueça isso, e leia o post ali.
Você deve usar ajax para conseguir o que quer.
>
22 minutos atrás, William Bruno disse:
Não cara.. esqueça isso, e leia o post ali.
Você deve usar ajax para conseguir o que quer.
Na parte do Cliente.class.php tenho a função gravar :
function gravar()
{
$objConexao = Conexao::getInstance();
try
{
$objConexao->beginTransaction();
$objData = new Data();
if($this->id_cliente == 0)
{
$sql = "INSERT INTO tb_cliente (";
$sql.= "id_representante, ";
$sql.= "tp_pessoa, ";
$sql.= "de_nome, ";
$sql.= "de_fantasia, ";
$sql.= "de_endereco, ";
$sql.= "de_cep, ";
$sql.= "de_bairro, ";
$sql.= "de_cidade, ";
$sql.= "de_uf, ";
$sql.= "de_ie, ";
$sql.= "de_cnpj, ";
$sql.= "de_rg, ";
$sql.= "de_cpf, ";
$sql.= "de_dddfone, ";
$sql.= "de_telefone, ";
$sql.= "de_dddfax, ";
$sql.= "de_fax, ";
$sql.= "de_email, ";
$sql.= "tp_ramo, ";
$sql.= "de_ramo, ";
$sql.= "de_comprador, ";
$sql.= "de_pagamento, ";
$sql.= "de_pagamento_outro, ";
$sql.= "tp_tipo_pgto, ";
$sql.= "id_transportadora, ";
$sql.= "de_transporte, ";
$sql.= "de_localentrega, ";
$sql.= "tp_tipo, ";
$sql.= "de_banco, ";
$sql.= "de_agencia, ";
$sql.= "de_conta, ";
$sql.= "de_nomef1, ";
$sql.= "de_endf1, ";
$sql.= "de_dddf1, ";
$sql.= "de_fonef1, ";
$sql.= "de_nomef2, ";
$sql.= "de_endf2, ";
$sql.= "de_dddf2, ";
$sql.= "de_fonef2, ";
$sql.= "de_nomef3, ";
$sql.= "de_endf3, ";
$sql.= "de_dddf3, ";
$sql.= "de_fonef3, ";
$sql.= "de_nomef4, ";
$sql.= "de_endf4, ";
$sql.= "de_dddf4, ";
$sql.= "de_fonef4, ";
$sql.= "de_nomef5, ";
$sql.= "de_endf5, ";
$sql.= "de_dddf5, ";
$sql.= "de_fonef5, ";
$sql.= "de_nomef6, ";
$sql.= "de_endf6, ";
$sql.= "de_dddf6, ";
$sql.= "de_fonef6, ";
$sql.= "de_cobranca, ";
$sql.= "st_desconto, ";
$sql.= "va_desconto, ";
$sql.= "de_lote ";
$sql.= ") VALUES (";
$sql.= ":id_representante, ";
$sql.= ":tp_pessoa, ";
$sql.= ":nome, ";
$sql.= ":fantasia, ";
$sql.= ":endereco, ";
$sql.= ":cep, ";
$sql.= ":bairro, ";
$sql.= ":cidade, ";
$sql.= ":uf, ";
$sql.= ":ie, ";
$sql.= ":cnpj, ";
$sql.= ":rg, ";
$sql.= ":cpf, ";
$sql.= ":dddfone, ";
$sql.= ":telefone, ";
$sql.= ":dddfax, ";
$sql.= ":fax, ";
$sql.= ":email, ";
$sql.= ":ramo_tipo, ";
$sql.= ":ramo, ";
$sql.= ":comprador, ";
$sql.= ":pagamento, ";
$sql.= ":pagamento_outro, ";
$sql.= ":tipo_pgto, ";
$sql.= ":id_transportadora, ";
$sql.= ":transporte, ";
$sql.= ":localentrega, ";
$sql.= ":tipo, ";
$sql.= ":banco, ";
$sql.= ":agencia, ";
$sql.= ":conta, ";
$sql.= ":nomef1, ";
$sql.= ":endf1, ";
$sql.= ":dddf1, ";
$sql.= ":fonef1, ";
$sql.= ":nomef2, ";
$sql.= ":endf2, ";
$sql.= ":dddf2, ";
$sql.= ":fonef2, ";
$sql.= ":nomef3, ";
$sql.= ":endf3, ";
$sql.= ":dddf3, ";
$sql.= ":fonef3, ";
$sql.= ":nomef4, ";
$sql.= ":endf4, ";
$sql.= ":dddf4, ";
$sql.= ":fonef4, ";
$sql.= ":nomef5, ";
$sql.= ":endf5, ";
$sql.= ":dddf5, ";
$sql.= ":fonef5, ";
$sql.= ":nomef6, ";
$sql.= ":endf6, ";
$sql.= ":dddf6, ";
$sql.= ":fonef6, ";
$sql.= ":cobranca, ";
$sql.= ":st_desconto, ";
$sql.= ":va_desconto, ";
$sql.= ":lote)";
$resp = $objConexao->prepare($sql);
$resp->bindValue(":id_representante", $this->id_representante);
$resp->bindValue(":tp_pessoa", $this->pessoa);
$resp->bindValue(":nome", $this->nome);
$resp->bindValue(":fantasia", $this->fantasia);
$resp->bindValue(":endereco", $this->endereco);
$resp->bindValue(":cep", $this->cep);
$resp->bindValue(":bairro", $this->bairro);
$resp->bindValue(":cidade", $this->cidade);
$resp->bindValue(":uf", $this->uf);
$resp->bindValue(":ie", $this->ie);
$resp->bindValue(":cnpj", $this->cnpj);
$resp->bindValue(":rg", $this->rg);
$resp->bindValue(":cpf", $this->cpf);
$resp->bindValue(":dddfone", $this->dddfone);
$resp->bindValue(":telefone", $this->telefone);
$resp->bindValue(":dddfax", $this->dddfax);
$resp->bindValue(":fax", $this->fax);
$resp->bindValue(":email", $this->email);
$resp->bindValue(":ramo_tipo", $this->ramo_tipo);
$resp->bindValue(":ramo", $this->ramo);
$resp->bindValue(":comprador", $this->comprador);
$resp->bindValue(":pagamento", $this->pagamento);
$resp->bindValue(":pagamento_outro", $this->pagamento_outro);
$resp->bindValue(":tipo_pgto", $this->tipo_pgto);
$resp->bindValue(":id_transportadora", $this->id_transportadora);
$resp->bindValue(":transporte", $this->transporte);
$resp->bindValue(":localentrega", $this->localentrega);
$resp->bindValue(":tipo", $this->tipo);
$resp->bindValue(":banco", $this->banco);
$resp->bindValue(":agencia", $this->agencia);
$resp->bindValue(":conta", $this->conta);
$resp->bindValue(":nomef1", $this->nomef1);
$resp->bindValue(":endf1", $this->endf1);
$resp->bindValue(":dddf1", $this->dddf1);
$resp->bindValue(":fonef1", $this->fonef1);
$resp->bindValue(":nomef2", $this->nomef2);
$resp->bindValue(":endf2", $this->endf2);
$resp->bindValue(":dddf2", $this->dddf2);
$resp->bindValue(":fonef2", $this->fonef2);
$resp->bindValue(":nomef3", $this->nomef3);
$resp->bindValue(":endf3", $this->endf3);
$resp->bindValue(":dddf3", $this->dddf3);
$resp->bindValue(":fonef3", $this->fonef3);
$resp->bindValue(":nomef4", $this->nomef4);
$resp->bindValue(":endf4", $this->endf4);
$resp->bindValue(":dddf4", $this->dddf4);
$resp->bindValue(":fonef4", $this->fonef4);
$resp->bindValue(":nomef5", $this->nomef5);
$resp->bindValue(":endf5", $this->endf5);
$resp->bindValue(":dddf5", $this->dddf5);
$resp->bindValue(":fonef5", $this->fonef5);
$resp->bindValue(":nomef6", $this->nomef6);
$resp->bindValue(":endf6", $this->endf6);
$resp->bindValue(":dddf6", $this->dddf6);
$resp->bindValue(":fonef6", $this->fonef6);
$resp->bindValue(":cobranca", $this->cobranca);
$resp->bindValue(":st_desconto", $this->st_desconto);
$resp->bindValue(":va_desconto", $this->desconto);
$resp->bindValue(":lote", $this->lote);
$resp->execute();
$ultimo_id = $objConexao->lastInsertId();
}
else
{
$sql = "UPDATE tb_cliente SET ";
$sql.= "id_representante = :id_representante, ";
$sql.= "tp_pessoa = :tp_pessoa, ";
$sql.= "de_nome = :nome, ";
$sql.= "de_fantasia = :fantasia, ";
$sql.= "de_endereco = :endereco, ";
$sql.= "de_cep = :cep, ";
$sql.= "de_bairro = :bairro, ";
$sql.= "de_cidade = :cidade, ";
$sql.= "de_uf = :uf, ";
$sql.= "de_ie = :ie, ";
$sql.= "de_cnpj = :cnpj, ";
$sql.= "de_rg = :rg, ";
$sql.= "de_cpf = :cpf, ";
$sql.= "de_dddfone = :dddfone, ";
$sql.= "de_telefone = :telefone, ";
$sql.= "de_dddfax = :dddfax, ";
$sql.= "de_fax = :fax, ";
$sql.= "de_email = :email, ";
$sql.= "tp_ramo = :ramo_tipo, ";
$sql.= "de_ramo = :ramo, ";
$sql.= "de_comprador = :comprador, ";
$sql.= "de_pagamento = :pagamento, ";
$sql.= "de_pagamento_outro = :pagamento_outro, ";
$sql.= "tp_tipo_pgto = :tipo_pgto, ";
$sql.= "id_transportadora = :id_transportadora, ";
$sql.= "de_transporte = :transporte, ";
$sql.= "de_localentrega = :localentrega, ";
$sql.= "tp_tipo = :tipo, ";
$sql.= "de_banco = :banco, ";
$sql.= "de_agencia = :agencia, ";
$sql.= "de_conta = :conta, ";
$sql.= "de_nomef1 = :nomef1, ";
$sql.= "de_endf1 = :endf1, ";
$sql.= "de_dddf1 = :dddf1, ";
$sql.= "de_fonef1 = :fonef1, ";
$sql.= "de_nomef2 = :nomef2, ";
$sql.= "de_endf2 = :endf2, ";
$sql.= "de_dddf2 = :dddf2, ";
$sql.= "de_fonef2 = :fonef2, ";
$sql.= "de_nomef3 = :nomef3, ";
$sql.= "de_endf3 = :endf3, ";
$sql.= "de_dddf3 = :dddf3, ";
$sql.= "de_fonef3 = :fonef3, ";
$sql.= "de_nomef4 = :nomef4, ";
$sql.= "de_endf4 = :endf4, ";
$sql.= "de_dddf4 = :dddf4, ";
$sql.= "de_fonef4 = :fonef4, ";
$sql.= "de_nomef5 = :nomef5, ";
$sql.= "de_endf5 = :endf5, ";
$sql.= "de_dddf5 = :dddf5, ";
$sql.= "de_fonef5 = :fonef5, ";
$sql.= "de_nomef6 = :nomef6, ";
$sql.= "de_endf6 = :endf6, ";
$sql.= "de_dddf6 = :dddf6, ";
$sql.= "de_fonef6 = :fonef6, ";
$sql.= "de_cobranca = :cobranca, ";
$sql.= "st_desconto = :st_desconto, ";
$sql.= "va_desconto = :va_desconto, ";
$sql.= "de_lote = :lote ";
$sql.= "WHERE id_cliente = :id_cliente";
$resp = $objConexao->prepare($sql);
$resp->bindValue(":id_representante", $this->id_representante);
$resp->bindValue(":tp_pessoa", $this->pessoa);
$resp->bindValue(":nome", $this->nome);
$resp->bindValue(":fantasia", $this->fantasia);
$resp->bindValue(":endereco", $this->endereco);
$resp->bindValue(":cep", $this->cep);
$resp->bindValue(":bairro", $this->bairro);
$resp->bindValue(":cidade", $this->cidade);
$resp->bindValue(":uf", $this->uf);
$resp->bindValue(":ie", $this->ie);
$resp->bindValue(":cnpj", $this->cnpj);
$resp->bindValue(":rg", $this->rg);
$resp->bindValue(":cpf", $this->cpf);
$resp->bindValue(":dddfone", $this->dddfone);
$resp->bindValue(":telefone", $this->telefone);
$resp->bindValue(":dddfax", $this->dddfax);
$resp->bindValue(":fax", $this->fax);
$resp->bindValue(":email", $this->email);
$resp->bindValue(":ramo_tipo", $this->ramo_tipo);
$resp->bindValue(":ramo", $this->ramo);
$resp->bindValue(":comprador", $this->comprador);
$resp->bindValue(":pagamento", $this->pagamento);
$resp->bindValue(":pagamento_outro", $this->pagamento_outro);
$resp->bindValue(":tipo_pgto", $this->tipo_pgto);
$resp->bindValue(":id_transportadora", $this->id_transportadora);
$resp->bindValue(":transporte", $this->transporte);
$resp->bindValue(":localentrega", $this->localentrega);
$resp->bindValue(":tipo", $this->tipo);
$resp->bindValue(":banco", $this->banco);
$resp->bindValue(":agencia", $this->agencia);
$resp->bindValue(":conta", $this->conta);
$resp->bindValue(":nomef1", $this->nomef1);
$resp->bindValue(":endf1", $this->endf1);
$resp->bindValue(":dddf1", $this->dddf1);
$resp->bindValue(":fonef1", $this->fonef1);
$resp->bindValue(":nomef2", $this->nomef2);
$resp->bindValue(":endf2", $this->endf2);
$resp->bindValue(":dddf2", $this->dddf2);
$resp->bindValue(":fonef2", $this->fonef2);
$resp->bindValue(":nomef3", $this->nomef3);
$resp->bindValue(":endf3", $this->endf3);
$resp->bindValue(":dddf3", $this->dddf3);
$resp->bindValue(":fonef3", $this->fonef3);
$resp->bindValue(":nomef4", $this->nomef4);
$resp->bindValue(":endf4", $this->endf4);
$resp->bindValue(":dddf4", $this->dddf4);
$resp->bindValue(":fonef4", $this->fonef4);
$resp->bindValue(":nomef5", $this->nomef5);
$resp->bindValue(":endf5", $this->endf5);
$resp->bindValue(":dddf5", $this->dddf5);
$resp->bindValue(":fonef5", $this->fonef5);
$resp->bindValue(":nomef6", $this->nomef6);
$resp->bindValue(":endf6", $this->endf6);
$resp->bindValue(":dddf6", $this->dddf6);
$resp->bindValue(":fonef6", $this->fonef6);
$resp->bindValue(":cobranca", $this->cobranca);
$resp->bindValue(":st_desconto", $this->st_desconto);
$resp->bindValue(":va_desconto", $this->desconto);
$resp->bindValue(":lote", $this->lote);
$resp->bindValue(":id_cliente", $this->id_cliente);
$resp->execute();
}
e eu tenho o arquivo
cadastrar-cliente.php
e
cadastrar-cliente-exec.php
Onde vou inserir o cod do "USUARIO.PHP" e "INDEX.PHP" ...
>
2 horas atrás, William Bruno disse:
Não cara.. esqueça isso, e leia o post ali.
Você deve usar ajax para conseguir o que quer.
A INDEX.PHP ficou assim:
<div class="form-group">
<label class="col-sm-2 control-label" for="cnpj">CNPJ</label>
<div class="col-sm-4">
<input type="text" name="cnpj" class="form-control" id="cnpj" value="<?php echo str_pad($objCliente->cnpj, 14, "0", STR_PAD_LEFT); ?>" placeholder="CNPJ">
</div>
<div id="resultado"> </div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(function(){ // declaro o início do jquery
$("input[name='cnpj']").on('blur', function(){
var cnpj = $(this).val();
$.get('duplicidade.php?cnpj=' + cnpj, function(data){
$('#resultado').php(data);
});
});
});// fim do jquery
</script>
A DUPLICIDADE.PHP (USUARIO.PHP) ficou assim:
<?php
//envio o charset para evitar problemas com acentos
header("Content-Type: text/html; charset=UTF-8");
$mysqli = new mysqli('mysql01.ryjoplastic2.hospedagemdesites.ws", "ryjoplastic2", "ryj0pl45t1c2');
$cnpj = filter_input(INPUT_GET, 'de_cnpj');
$sql = "SELECT * FROM `tb_cliente` WHERE `de_cnpj` = '{$cnpj}'"; //monto a query
$query = $mysqli->query( $sql ); //executo a query
if( $query->num_rows > 0 ) {//se retornar algum resultado
echo 'Já existe!';
} else {
echo 'Não existe ainda!';
}
?>
NÃO ME DA NENHUM RETORNO ESTES COMANDOS...aperte CTRL + SHIFT + J no teu Firefox ou Chrome e veja quais erros aparecem, corrija eles.
Além disso, veja se o teu php está correto, indo pelo browser:
/duplicidade.php?cnpj=111...
corrija os erros que aparecer.
@rafinha_sam fazer o bindvalue manualmente juntamente com a query pode levar horas de trabalho
Siga os comentários deste tópico e tenha seu insert feito 'automaticamente'
Conteudo migrado sem texto.
Leia:
http://wbruno.com.br/ajax/verificar-se-usuario-ja-existe-no-banco/