Avoiding duplicate data by applying counter for usage log

5

In a simple form that writes the following data:

  

ID, IP, Date / Time, Name, Email, State / City.

When a person fills out the form, this data is recorded in the database. But if the same user is to fill out the form again, a new record will be recorded, creating a doubling of information.

How can I control this scenario so that if the same person uses the form with the same data, instead of duplicating the record, just increment a control counter?

    
asked by anonymous 29.01.2015 / 00:36

2 answers

1

The correct logic is to use one of these ID, IP, Data/Hora, Nome, Email e Estado/Cidade fields as a validator . You can use a sort of validation using campo Email , for example, as a reference. The ip is out of the question, since inside a house 2 or more people can use the same ip:

Example:

$email = $_POST['email'];

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

$duperaw = mysql_query($dupesql);

if (mysql_num_rows($duberaw) > 0) {

  //FIM DO CADASTRO
  //AVISA QUE JÁ POSSUI UM EMAIL COM ESTES DADOS  
}else{

  //CONTINUA O CADASTRO


}
    
29.01.2015 / 15:04
1

First of all I suggest that you create a unique key with fields that can not be duplicated, so MySQL itself prevents duplication.

For example:

UNIQUE INDEX 'nome_da_chave' ('nome', 'email', 'cidade')

With this key created when trying to insert a duplicate record the query will generate an error 1022 SQLSTATE: 23000 ( link )

Before inserting a new record you should consult the table, if there is no record you can enter, otherwise update.

    
06.02.2015 / 12:09