Error verifying user duplication

2

I wanted to prohibit the user from registering two identical users, type two equal emails or enrollment, but is still being registered data already exists, After I press register and send the data to the bank appears this error:

  

Notice: Undefined index: total in C: \ xampp \ htdocs \ nsi \ registry.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>
    
asked by anonymous 31.10.2018 / 20:50

1 answer

5

I've created a simplified bank 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've changed the code to make it simpler to see

<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";
  }
}
?>

Result!

    
01.11.2018 / 17:58