How to avoid duplicate registration in a php form

2

I would like examples of how to avoid duplicate email if a user attempts to make multiple registrations.

 <form action="processar.php" method="POST">
      <input type="text" name="nome">
      <input type="email" name="email">
      <input type="submit">
   </form>

Process.php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "experiments";


//Pega os dados da form

   $nome = $_POST['nome'];
   $email = $_POST['email'];

 // Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection


$dupesql = "SELECT * FROM formulario where (email = '$email')";

$duperaw = mysql_query($dupesql);

if (mysql_num_rows($duberaw) > 0) {
   echo "Não deu certo, este e-mail já está cadastrado.";

} else {

$sql = "INSERT INTO formulario (nome, email)
VALUES ('$nome', '$email')";

}
    
asked by anonymous 02.06.2015 / 21:46

3 answers

1

No select, instead of searching by name, you search by email.

$dupesql = "SELECT * FROM formulario where (email = '$email')";

You can complement this by making the email primary key in the database database. That way, even if the user tries, they will not be able to register a duplicate email in their bank.

    
02.06.2015 / 21:51
1

You are creating the connection with mysqli_connect and running with mysql_query , repair the lack of i . Try changing if you still have an error, please modify your query to:

$dupesql = "SELECT * FROM formulario where email='$email'";
    
03.06.2015 / 03:46
0

I would remove the parentheses after the where:

The variables are different. You use $duperaw and $duberaw .

Check out the changes already:

$dupesql = "SELECT email FROM formulario where email = '$email'";

$duperaw = mysql_query($dupesql);

if (mysql_num_rows($duperaw) > 0) {
//mensagem de erro //
}

else {
// continui //
}
    
10.08.2015 / 13:53