Error when checking user duplication

Asked

Viewed 113 times

2

I wanted to prohibit the user from registering two iguas users, like two identical emails or matricula, but is still being recorded existing data, After that press in register and send the data to the database appears this error:

Notice: Undefined index: total in C: xampp htdocs nsi registro.php on line 20

    <?php 

include("includes/conexao.php");//conexão com o banco

if(isset($_POST["registrar"])) {
    $querySelect = "SELECT * FROM usuarios WHERE matricula = ?  or email = ?";
   $statement = $connection->prepare($querySelect);
   $statement->bindValue(1, $_POST['matricula']);
   $statement->bindValue(2, $_POST['email']);
   $statement->execute();        
   $total = $statement->rowCount();

    if($total == 0){

        $senha           = $_POST['senha'];
        $confirma_senha  = $_POST['confirma_senha'];
        if (empty($senha)) {
            $mensagem = "<span class='aviso'><b>Aviso</b>: Senha não foi alterada!</span>";
        } else if ($senha == $confirma_senha) {
            $query = "INSERT INTO usuario (nome, sobrenome, matricula, email, senha) 
                  VALUES (:nome, :sobrenome, :matricula, :email, :senha)";

            $statement = $connection->prepare($query);


            $valores = array();
            $valores[':nome'] = (isset($_POST['primeiroNome']) ? $_POST['primeiroNome'] : '');
            $valores[':sobrenome'] = (isset($_POST['sobrenome']) ? $_POST['sobrenome'] : '');
            $valores[':matricula'] = (isset($_POST['matricula']) ? $_POST['matricula'] : '');
            $valores[':email'] = (isset($_POST['email']) ? $_POST['email'] : '');
            $valores[':senha'] = (isset($_POST['senha']) ? $_POST['senha'] : '');

            $result = $statement->execute($valores);
            if(!empty($result)){
                $mensagem = "<span class='aviso'><b>Sucesso</b>: deu certo!</span>";
            }
        } else {
             $mensagem = "<span class='aviso'><b>Aviso</b>: Senha e repetir senha são diferentes!</span>";

        }

    }else{
        $mensagem = "<span class='aviso'><b>Erro</b>: Email ou matricula ja cadastrado!</span>";
    }

}


?>

If you want to see how the form is

<form method="POST" action="registro.php" onsubmit="return validarSenha()"name="formulario">
    <div class="form-group">
        <div class="form-row">
            <div class="col-md-6">
                <label for="primeiroNome">Primeiro nome</label>
                <input type="text" class="form-control" id="primeiroNome" name="primeiroNome" placeholder="Digite seu primeiro nome" required="required">
            </div>
            <div class="col-md-6">
                <label for="Sobrenome">Sobrenome</label>
                <input type="text" class="form-control" id="Sobrenome" name="sobrenome" placeholder="Digite seu Sobrenome" required="required" autofocus="autofocus">
            </div>
        </div>
    </div>
    <div class="col-md-6" id="matricula">
        <label for="primeiroNome">Matrícula</label>
        <input type="text" class="form-control"  name="matricula" placeholder="Digite sua matrícula" required="required" autofocus="autofocus">
    </div>
    <div class="form-group">
        <label for="email">E-mail</label>
        <input type="email" class="form-control" id="email" name="email" placeholder="Digite seu email" required="required" autofocus="autofocus">
    </div>
    <div class="form-group">
        <div class="form-row">
            <div class="col-md-6">
                <label for="senha">Senha</label>
                <input type="password" class="form-control" id="senha" name="senha" placeholder="Digite sua senha" required="required" autofocus="autofocus">
            </div>
            <div class="col-md-6">
                <label for="confirma_senha">confirmar senha</label>
                <input type="password"  class="form-control" id="confirmaSenha" name="confirma_senha" placeholder="Confirme sua senha" required="required" autofocus="autofocus">
            </div>
        </div>
    </div>
    <div>
        <input type="submit" class="btn btn-primary btn-block" name="registrar" value="Registra-se"/>
    </div>
    <div class="text-center">
        <a href="#" class="d-block small mt-3">Esqueceu sua senha?</a>
        <a href="login.php" class="d-block small mt-3">Login?</a>
    </div>
</form>
  • So far I’ve removed my answer, I’m kind of busy right now.

  • 2

    All right, thanks for your attention!!!

1 answer

5


I created a simplified database to test and show you the result.

create table usuarios(
cod int primary key auto_increment,
matricula varchar(500),
email varchar(500)
);

INSERT INTO usuarios(matricula, email) VALUES('4254125','[email protected]');

I changed the code to make it simpler to view

<form method="POST" action="index.php" name="formulario">
  <div class="col-md-6" id="matricula">
    <label for="primeiroNome">Matrícula</label>
    <input type="text" class="form-control"  name="matricula" placeholder="Digite sua matrícula" required="required" autofocus="autofocus">
  </div>
  <div class="form-group">
    <label for="email">E-mail</label>
    <input type="email" class="form-control" id="email" name="email" placeholder="Digite seu email" required="required" >
  </div>
</div>
</div>
<div>
  <input type="submit" class="btn btn-primary btn-block" name="registrar" value="Registra-se"/>
</div>
</form>
<?php
$connection = new PDO ( "mysql:host=localhost;dbname=dbrp2", "root", "" ) or die ("Erro ao conectar no banco");

if(isset($_POST["registrar"])) {
  $querySelect = "SELECT * FROM usuarios WHERE matricula = ?  OR email = ?";
  $statement = $connection->prepare($querySelect);
  $statement->bindValue(1, $_POST['matricula']);
  $statement->bindValue(2, $_POST['email']);
  $statement->execute();
  $total = $statement->rowCount();

  if($total == 0){
    echo "NADA ENCONTRADO";
  }else{
  echo "REGISTRO DUPLICADO";
  }
}
?>

Upshot!

inserir a descrição da imagem aqui

  • Continues with the same error, is still being inserted in the bank.

  • Are you sure you are testing with an existing registration or email? the OR operator is one or the other. If none of them are in the bank will enter. I tested this code and it’s working.

  • 1

    Try showing the $total variable in echo comment the rest and see what is returning.

  • I put it the way you said, but it didn’t work

  • Looks like it turned out, but still keeps recording existing data

  • I changed the answer

  • Check if it is connected to the right bank. This could be it

Show 2 more comments

Browser other questions tagged

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