1 Reply Latest reply: Sep 24, 2011 1:57 AM by EJP RSS

    Applet to PHP recommended practice

    889476
      I have an Applet, which adds lines to a database one at a time, by sending an SQL string via AJAX to a PHP script. I have 3 questions:

      1. Is is good practice to use a string here, or should I try to do something else?
      2. Is there a limit on how long the string should be?
      3. Is there a limit on how often it should be sent?

      The Applet data insertion method is as follows:
      private void addItem3(String newWord) {
      if(LIVE) {
      if(jso != null )
      try {
      jso.call("updateWebPage", new String[] {newWord});
      }
      catch (Exception ex) {
      addItem2("jso call failed... ");
      ex.printStackTrace();
      }
      }
      }
      Where the parameter newWord being passed to that method might look like:
      INSERT INTO Items (Partid, OpCode, ItemLeft, ItemRight, Raw, Rate) VALUES (684, 1, 2, 5, 1, 34)
      The JavaScript function updateWebPage is:
      <script type="text/javascript">
      function updateWebPage(myArg)
      {
      document.getElementById("txt1").innerHTML=myArg;
      if (myArg=="")
      {
      document.getElementById("cbxItem").innerHTML="";
      return;
      }
      if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
      else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
      document.getElementById("cbxItem").innerHTML=xmlhttp.responseText;
      }
      }
      xmlhttp.open("GET","putitem.php?id="+myArg,true);
      xmlhttp.send();
      }
      </script>
      And the php code is:
      <?php
      $id = $_GET['id'];
      include('dbinfo.php');// collect database variables and connect.
      $con = mysql_connect( $dbhost, $dbuser, $dbpass );
      if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
      mysql_select_db($dbname, $con);
      // first use encodeURIComponent on javascript to encode the string
      // receive json string and prepare it to json_decode
      $jsonStr = stripslashes ($id);
      $query = $jsonStr;
      $result = mysql_query($query) or die(mysql_error());
      echo"<p>The query is: $query </p>";
      echo"<p>The result is: $result </p>";
      mysql_close($con);
      ?>
      The thing is, even a single instance of the Applet can generate new lines very quickly, sometimes once a second. So a classroom of 25 students might generate 25 lines a second. I am concerned at how my ISP server may react to all these requests to open a connection, insert a single line of data, and close the connection.

      It occurs to me, I could aggregate the insert instructions, say in groups of five, so the parameter being passed would look more like:
      INSERT INTO Items (Partid, OpCode, ItemLeft, ItemRight, Raw, Rate) VALUES (684, 1, 2, 5, 1, 34),(684, 1, 2, 3, 1, 25),(684, 1, 2, 6, 1, 28),(684, 1, 4, 5, 1, 21),(684, 1, 3, 8, 1, 19)
      Or maybe I could group 10, or 15, or 20.

      This is all completely new to me, so I'd appreciate any input at all.