Inform the user that the registration already exists

Asked

Viewed 281 times

0

I have a code to register the user on my site, he sees that the user is already registered and does not enter again in the table, but I can not inform the user that the registration already exists.

    $sql = 'INSERT INTO usar (campo1, campo2) VALUES (?,?)';


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


  if(!$stmt){
      echo 'erro na consulta: '. $conn->error .' - '. $conn->error; 
  }
    $var1 = $_POST['campo1'];
    $var2 = $_POST['campo2'];


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

    echo"<script type='text/javascript'>alert('Cadastro realizado com sucesso.');window.location.href='cadastro.php';</script>";
  • Make a select before giving the Insert, see if the email he tried to register already exists in the bank, if the return is true you inform the user that the email already exists...

  • I tried, but it still won’t go

  • 1

    Some field is key Unique, email for example.

  • And how I change that?

  • How do you see it? /Where is the part of the code that he sees that the user is already registered?

  • Guys, thank you for your attention, but I think I solved it. I’ll post the answer for you to see and if there’s anything wrong, if possible, let me know.

Show 1 more comment

1 answer

2


I resolved so:

  $var1 = $_POST['campo1'];
  $var2 = $_POST['campo2'];

  $query = "SELECT * FROM usar WHERE campo1 = '$var1' AND campo2 = '$var2'";

      $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 usar (campo1, campo2) VALUES (?,?)';


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


  if(!$stmt){
      echo 'erro na consulta: '. $conn->error .' - '. $conn->error; 
  }
    $var1 = $_POST['campo1'];
    $var2 = $_POST['campo2'];


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

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

    And this is not what was said in the comment of Darlei Fernando Zillmer ?? But I think it will continue to enter again same registration. Missed you put an Else to perform the registration.

  • Exactly. I was able to solve it with his answer. Is there any way to put or mention it in the answer? I don’t know how to move much here yet

Browser other questions tagged

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