0

I recently notice that I have a duplicate line on the table, when the device is spinning or calling someone on this davay at the time of pressing the 'save' button. On the lines of UserRealTime I see that the interval is a duplicate of 5-6 milliseconds.

How to avoid duplicates using javascript or jQuery. For example, check the connection of the device to the Internet?

ajax.php

<?php
if (isset($_GET['d1']) && isset($_GET['d2']))
{
  $conn=connSQL();
  $query = "insert into doorname(d1, d2, UserRealTime) values ('".$_GET['d1']."','".$_GET['d2']."', getdate())";
  $rs=$conn->execute($query);
  $rs->Close();
  $conn->Close();
}
?>

JavaScript

<script>
var httpObject = null;

function getHTTPObject()
{
  if (window.ActiveXObject)
  {
      return new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if(window.XMLHttpRequest)
  {
     return new XMLHttpRequest();
  }
  else
  {
        return null;
  }
}

function Inter()
{
  httpObject = getHTTPObject();
  if (httpObject != null)
  {
    var d1=document.getElementById('d1').value;
    var d2=document.getElementById('d2').value;
    if (d1=="" || d2=="")
    {
        alert("sorry!!!");
    }
    else
    {
         httpObject.open("GET", "ajax.php?d1="+d1+"&d2="+d2, true);
         httpObject.send(null);
         httpObject.onreadystatechange = InterSet;
    }
  }
}

function InterSet()
{
  if(httpObject.readyState == 4)
  {
    var data=httpObject.responseText;
    alert("Good!!!");
  }
}

</script>
  • 3
    **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Feb 27 '18 at 15:59
  • And what is your question? – Nico Haase Feb 27 '18 at 16:03
  • How to avoid duplicates using javascript or jQuery. For example, check the connection of the device to the Internet? – Forsov Forsov Feb 27 '18 at 16:10

1 Answers1

0

This way of approach it is not a suggested practice. However, just to work around the problem here is one way of handling general duplicate DB entries.

Generate one random Token on the client side for every successful server side insert. Discard the Token on client side once the server confirms the successful insert. Following is an example:

1) Generate a random Token on the client side, like so Generate random string/characters in JavaScript

var tokenOnClientSide = makeid();

2) Attach the generated Token to Ajax Params:

httpObject.open("GET", "ajax.php?d1="+d1+"&d2="+d2+"&token="+token, true);

3) Server side: Look if the Token already exists

<?php
if (isset($_GET['d1']) && isset($_GET['d2']) && isset($_GET['token']))
{
    $query = sprintf("SELECT * FROM token_table WHERE token = '%s'", $_GET['token']);
    $rs=$conn->execute($query);
    if (mysql_num_rows($res) == 0) {
        // carry on with insert and return success message
        .....

        // now store the token permanently
        $query = "insert into token_table(token, time_of_exec) values ('".$_GET['token']."', getdate())";
        $rs=$conn->execute($query);
    }

4) Finally, unset the global token on client side

tokenOnClientSide = "";

Hope that one gets a basic idea of handling duplicates.

Odd
  • 563
  • 8
  • 20