Avoid duplicate data by applying use log counter

Asked

Viewed 1,801 times

5

In a simple form that saves the following data:

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

When a person fills in the form, this data is recorded in the database. But if the same user fills in the form again, a new record will be registered, creating a duplication 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, increment only one counter ?

  • What type of database connection you are using? PDO, mysql, mysqli ?

  • Have you tried going to W3school, learning the basics of Sqli ? And you can also avoid this by using SQL using Unique. IP => $_SERVER['REMOTE_ADDR']; data => date(); hora = time(); // you can always create a function to write in Portuguese.

2 answers

1

First I suggest you create a unique key with fields that cannot be duplicated, so Mysql itself avoids duplicity.

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 (http://dev.mysql.com/doc/refman/5.1/en/error-messages-server.html#error_er_dup_key)

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

1

The correct logic is to use one of these fields ID, IP, Data/Hora, Nome, Email e Estado/Cidade as a validator. You can use a kind of validation using the 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


}
  • I removed the quote because I deleted the answer.Okay? I hadn’t noticed the part he asks to increment a counter

  • okay..............

Browser other questions tagged

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