Error in searching the register in the comic

Asked

Viewed 31 times

0

I made a registration page on my website, which to register needs Cpf and email. I made a SELECT to see if it already exists, if yes, do not enter, otherwise do not enter. I left Cpf as pk and email as Unique. When I put an existing Cpf it shows the Alert I did saying that 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 the SELECT that I did, I tried to put AND, but it got worse, I didn’t even check if Cpf was there, I always completed the register. You can see some mistake?

  • 2

    pq does not make a query with only one OR verifying email and cpf If you have any record it is because either of the two already exists. There is a lot of code repeated there O.o

  • Really kkkkk my code gets a mess at first, but then I fix everything

2 answers

2

You are performing only one reading, in case you are replacing:

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

for

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

Do it this way:

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

1


When you do that:

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

You say that the variable is receiving a value, in case using the same variable, you will be missing the first select that will never run, so create two variables, two queries, add more conditional, or simply do with an OR in the 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, a studied in programming logic, I think it can help a lot

Browser other questions tagged

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