How do I not insert repeated records?

5

I am doing a login system in which each user creates a URL that sends to the bank, but I need to make it not repeat information, if there is already information in the database, it returns (URL indisponível) result. p>

INPUT CODE:

MEUSITE.COM/<input class="subbutton" name="url" type="text" value="<? echo("$prof[url]");?>" size="40">


PHP:

<? }

elseif($update==update)
{
$nomedoevento = clean($_POST[nomedoevento]);
$descricaodoevento = clean($_POST[descricaodoevento]);
$datadoevento = clean($_POST[datadoevento]);
$horadoevento = clean($_POST[horadoevento]);
$localdoevento = clean($_POST[localdoevento]);
$enderecodoevento = clean($_POST[enderecodoevento]);
$bairrodoevento = clean($_POST[bairrodoevento]);
$cidadedoevento = clean($_POST[cidadedoevento]);
$estadodoevento = clean($_POST[estadodoevento]);
$habilitarevento = clean($_POST[habilitarevento]);
$url = clean($_POST[url]);
$updatenomedoevento = mysql_query("update usr_users set nomedoevento = '$nomedoevento', descricaodoevento = '$descricaodoevento', datadoevento = '$datadoevento', horadoevento = '$horadoevento', localdoevento = '$localdoevento', enderecodoevento = '$enderecodoevento', bairrodoevento = '$bairrodoevento', cidadedoevento = '$cidadedoevento', estadodoevento = '$estadodoevento', habilitarevento = '$habilitarevento', url = '$url' where email = '$_SESSION[usr_email]'");
    echo("Suas informações foram atualizadas!");
} ?>
    
asked by anonymous 01.07.2014 / 01:55

2 answers

5

There will always be some "crazy" proposing to give SELECT to see if a record exists to insert later, nor waste time with these solutions because usually those who do not have a clue what they are doing. Nothing guarantees that there will not be an insertion by another process between SELECT and INSERT following.

What we need (after all, what is the goal to be achieved) is to avoid inserting duplicate records, and the DB already has the correct mechanism for it, which is the unique key.

First, you should create a UNIQUE index for the URL column. This is your guarantee of oneness.

This applies to you. But if you were in another situation, where two or more columns could not repeat, such as a doctor's schedule, you would create an index using the doctor's ID and the timetable, for example (the same timetable could be used by two different doctors, or the same doctor attending two different times, but never two schedules with same time and same doctor). UNIQUE is not limited to just one column.

Once your UNIQUE is created, just compare the MySQL error with the value 1062 to see if the URL is repeated:

    // A versão original da resposta usava funções mysql,
    // que já foram removidas do PHP. 

    $useremail = $_SESSION[usr_email]; // pra usar na query

    mysqli_query ( $db, " 
       UPDATE usr_users SET
          nomedoevento = '$nomedoevento',
          descricaodoevento = '$descricaodoevento',
          datadoevento = '$datadoevento',
          horadoevento = '$horadoevento',
          localdoevento = '$localdoevento',
          enderecodoevento = '$enderecodoevento',
          bairrodoevento = '$bairrodoevento',
          cidadedoevento = '$cidadedoevento',
          estadodoevento = '$estadodoevento',
          habilitarevento = '$habilitarevento',
          url = '$url'
       WHERE email = '$useremail'
    ");

    $errno = mysqli_errno($db); 

    if ( $errno == 0 ) {
       echo( 'Suas informacoes foram atualizadas!' );
    } else if ( $errno == 1062 ) {
       echo( 'Este URL ja existe!' );
    } else {
       echo( 'Ocorreu um erro: ' . mysqli_error($db) );
    }
  

Note that I did not set up your code to avoid SQL Injection because it was not the focus of the question. As it is, a malicious form can send data to delete your DB, or even steal information.

If you want, you can change if to switch .

    
01.07.2014 / 03:44
0

What would I do?

I would do a query in the database, compare the values and then after the check would do the update.

if($url == $resultadoConsulta['url']){

print "<script>alerta('URL indisponivel')<script/>";


 }else{

 Toca ficha no update!

 }

And you can use the other values.

    
01.07.2014 / 02:26