Inform the user that the registration already exists

0

I have a user registration code on my site, it sees that the user is already registered and does not re-enter 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>";
    
asked by anonymous 27.11.2017 / 14:34

1 answer

2

I solved it like this:

  $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>";
    
27.11.2017 / 15:10