Error in registering the query in bd

0

I made a registration page on my site, which to do the registration needs cpf and email. I made a SELECT to see if it already exists, if yes, not insert, otherwise it does not insert. I left the cpf as pk and email as unique. When I put an existing cpf it shows the alert that I did saying it already exists, but when it is a new cpf and email that is already in the bd, it does not inform and register.

    $var1 = $_POST['cpf'];
    $var2 = $_POST['email'];

  $query = "SELECT * FROM teste WHERE email = '$var2'";
  $query = "SELECT * FROM teste WHERE cpf = '$var1'";


      $querySelect = mysqli_query($conn, $query);

        if (mysqli_num_rows($querySelect) > 0) {
          echo"<script type='text/javascript'>alert('Cadastro existente.');window.location.href='cadastro.php';</script>";
        }

            $var1 = $_POST['cpf'];
            $var2 = $_POST['email'];


            $sql = 'INSERT INTO teste (cpf, email) VALUES (?,?)';

            $stmt = $conn->prepare($sql);

            $var1 = $_POST['cpf'];
            $var2 = $_POST['email'];


            $stmt->bind_param('ss', $var1, $var2);
            $stmt->execute();

            echo"<script type='text/javascript'>alert('Cadastro realizado com sucesso.');window.location.href='index.php';</script>";

            if(!$stmt){
              echo 'erro na consulta: '. $conn->error .' - '. $conn->error;
            }

And in% w / o I did, I tried to put AND, but it got worse, I did not even check if the cpf was there, I always ended the registration. Can you see any errors?

    
asked by anonymous 06.12.2017 / 14:12

2 answers

1

When you do this:

$query = "SELECT * FROM teste WHERE email = '$var2'";
$query = "SELECT * FROM teste WHERE cpf = '$var1'";

You say that the variable is receiving a value, in this case using the same variable, you are losing the first select that will never be executed, then create two variables, two queries, add more conditionals, or simply do with an OR in query:

$query = "SELECT * FROM teste WHERE email = '".$var2."' OR cpf = '".$var1."'";

Complete code:

$var1 = $_POST['cpf'];
$var2 = $_POST['email'];

$query = "SELECT * FROM teste WHERE email = '".$var2."' OR cpf = '".$var1."'";
$querySelect = mysqli_query($conn, $query);
if (mysqli_num_rows($querySelect) > 0) {
    echo"<script type='text/javascript'>alert('Cadastro existente.');window.location.href='cadastro.php';</script>";
}
$sql = 'INSERT INTO teste (cpf, email) VALUES (?,?)';
$stmt = $conn->prepare($sql);
$stmt->bind_param('ss', $var1, $var2);
$stmt->execute();

echo"<script type='text/javascript'>alert('Cadastro realizado com sucesso.');window.location.href='index.php';</script>";

if(!$stmt){
    echo 'erro na consulta: '. $conn->error .' - '. $conn->error;
}
  

Note: As rray said, there is a lot of duplicate code there, from the one studied   in programming logic, I think it can help a lot

    
06.12.2017 / 14:18
1

You are only doing one reading, in this case, you are replacing:

 $query = "SELECT * FROM teste WHERE email = '$var2'";

by

  $query = "SELECT * FROM teste WHERE cpf = '$var1'";

Do as follows:

$query = "SELECT * FROM teste WHERE email = '$var2' OR cpf='$var1';
    
06.12.2017 / 14:17