Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    domanda su javascript (query instabile)

    Salve a tutti,
    Vengo subito al problema. Questa query non è "sicura"
    codice:
    mysql_query("SELECT COUNT(*) AS count FROM RegistrazioneSchedine WHERE data= '$data' AND Nickname= '$_GET[username]'");
    mi restituisce a volte valori non veri, ho notato che a volte inizia con zero ed è impossibile perchè quando viene effettuata c'è già almeno una registrazione, poi a volte è un valore in meno di quelli realmente registrati, però alla registrazione successiva ritorna alla normalità facendo salti (dipende quanti valori non ha contato precedentemente)

    Questo è il codice:
    codice:
    			function getHTTPObject(){
    			   if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
    			   else if (window.XMLHttpRequest) return new XMLHttpRequest();
    			   else {
    				  alert("Your browser does not support AJAX.");
    				  return null;
    			   }
    			}   
     
    			function setOutput(){
    				if(httpObject.readyState == 4){
    					document.getElementById('numero1').value = httpObject.responseText;	
    					document.getElementById('numero2').value = httpObject.responseText;
    					document.getElementById('numero3').value = httpObject.responseText;
    					document.getElementById('numero4').value = httpObject.responseText;
    					document.getElementById('numero5').value = httpObject.responseText;									
    				}
    			 
    			}
    			 
    			function doWork(){    
    				httpObject = getHTTPObject();
    				if (httpObject != null) {
    					httpObject.open("GET", "components/com_jumi/files/elabora.php?username="
    									+document.getElementById('username').value+"&numero1="
    									+document.getElementById('numero1').value+"&numero2="
    									+document.getElementById('numero2').value+"&numero3="
    									+document.getElementById('numero3').value+"&numero4="
    									+document.getElementById('numero4').value+"&numero5="
    									+document.getElementById('numero5').value, true);
    					httpObject.send(null); 
    					httpObject.onreadystatechange = setOutput;
    				}
    				
    				
    				function setOutput2(){
    					if(httpObject.readyState == 4){
    						document.getElementById('ns').value = httpObject.responseText;
    						var ns =  document.getElementById('ns').value;
    						if(ns>=1){
    							document.getElementById("n1").style.background="url(components/com_jumi/files/blue.png)";}
    						if(ns>=2){
    							document.getElementById("n2").style.background="url(components/com_jumi/files/blue.png)";}
    						if(ns>=3){
    							document.getElementById("n3").style.background="url(components/com_jumi/files/blue.png)";}
    						if(ns>=4){
    							document.getElementById("n4").style.background="url(components/com_jumi/files/blue.png)";}
    						if(ns>=5)
    							document.getElementById("n5").style.background="url(components/com_jumi/files/blue.png)";
    						if(ns>=6)
    							document.getElementById("n6").style.background="url(components/com_jumi/files/blue.png)";
    						if(ns>=7)
    							document.getElementById("n7").style.background="url(components/com_jumi/files/blue.png)";
    						if(ns>=8)
    							document.getElementById("n8").style.background="url(components/com_jumi/files/blue.png)";
    						if(ns>=9)
    							document.getElementById("n9").style.background="url(components/com_jumi/files/blue.png)";
    						if(ns>=10)
    							document.getElementById("n10").style.background="url(components/com_jumi/files/blue.png)";
    						if(ns>=11)
    							document.getElementById("n11").style.background="url(components/com_jumi/files/blue.png)";
    						if(ns>=12)
    							document.getElementById("n12").style.background="url(components/com_jumi/files/blue.png)";
    						if(ns>=13)
    							document.getElementById("n13").style.background="url(components/com_jumi/files/blue.png)";
    						if(ns>=14)
    							document.getElementById("n14").style.background="url(components/com_jumi/files/blue.png)";
    						if(ns>=15)
    							document.getElementById("n15").style.background="url(components/com_jumi/files/blue.png)";
    						if(ns>=16)
    							document.getElementById("n16").style.background="url(components/com_jumi/files/blue.png)";
    						if(ns>=17)
    							document.getElementById("n17").style.background="url(components/com_jumi/files/blue.png)";
    						if(ns>=18)
    							document.getElementById("n18").style.background="url(components/com_jumi/files/blue.png)";
    						if(ns>=19)
    							document.getElementById("n19").style.background="url(components/com_jumi/files/blue.png)";
    						if(ns==20)
    							document.getElementById("n20").style.background="url(components/com_jumi/files/blue.png)";
    								
    					} 
    				 
    				}	//fine setOutput2
    				
    				
    			    
    				httpObject = getHTTPObject();
    				
    				if (httpObject != null) {
    					httpObject.open("GET", "components/com_jumi/files/ns.php?username="
    									+document.getElementById('username').value, true);
    					httpObject.send(null); 
    					httpObject.onreadystatechange = setOutput2;
    					
    				}
    			
    				
    				
    				
    				
    			}//fine doWork
    			 
    			var httpObject = null;
    ns.php
    Codice PHP:
    <?php 
        $db_host 
    "****"
        
    $db_user "****"
        
    $db_password "****"
        
    $db_name "****";  
                         
                
    $db mysql_connect($db_host$db_user$db_password);
                      
    mysql_select_db($db_name$db);

                
    $data=date("d-m-Y");
            
            
    $query mysql_query("SELECT COUNT(*) AS count FROM RegistrazioneSchedine WHERE data= '$data' AND Nickname= '$_GET[username]'");     
            
    $sql mysql_fetch_array($query);  
            
    $_GET['ns'] = $sql['count'];
                        
        
    mysql_close($db); 
               
        if (isset(
    $_GET['ns']))
            echo 
    $_GET['ns'];
       
     
    ?>
    elabora.php
    Codice PHP:
    <?php 


        $db_host 
    "****"
        
    $db_user "*****"
        
    $db_password "****"
        
    $db_name "*****";                   
                
    $db mysql_connect($db_host$db_user$db_password);
                      
    mysql_select_db($db_name$db);

                
    $data=date("d-m-Y"); $ora=date("H:i:s");

            
    mysql_query("INSERT INTO RegistrazioneSchedine(Nickname, numero1, numero2, numero3, numero4, numero5, data, ora) 
                         VALUE ('
    $_GET[username]', '$_GET[numero1]', '$_GET[numero2]', '$_GET[numero3]', '$_GET[numero4]', '$_GET[numero5]', '$data', '$ora')");
                        
        
    mysql_close($db); 
               
        if (isset(
    $_GET['numero1']))
            echo 
    '';    

        if (isset(
    $_GET['numero2']))
            echo 
    '';    

        if (isset(
    $_GET['numero3']))
            echo 
    '';    

        if (isset(
    $_GET['numero4']))
            echo 
    '';    
            
        if (isset(
    $_GET['numero5']))
            echo 
    '';    
        
    ?>
    secondo me effettua la query
    codice:
    $query = mysql_query("SELECT COUNT(*) AS count FROM RegistrazioneSchedine WHERE data= '$data' AND Nickname= '$_GET[username]'"); $sql = mysql_fetch_array($query); $_GET['ns'] = $sql['count'];
    prima che viene salvata la query
    codice:
    mysql_query("INSERT INTO RegistrazioneSchedine(Nickname, numero1, numero2, numero3, numero4, numero5, data, ora) VALUE ('$_GET[username]', '$_GET[numero1]', '$_GET[numero2]', '$_GET[numero3]', '$_GET[numero4]', '$_GET[numero5]', '$data', '$ora')");
    quindi dipende tutto dai tempi di queste due...se il db non fa in tempo a salvare prima la seconda query che ho scritto avrà ns con un valore in meno...spero di essere stato chiaro..

    ringrazio anticipatamente chi mi risponderà

  2. #2
    Moderatore di JavaScript L'avatar di br1
    Registrato dal
    Jul 1999
    Messaggi
    19,998
    Scusa, potresti sintetizzare il problema javascript ? in mezzo a tutto quel codice in linguaggio estraneo a questo forum non mi ci raccapezzo

    ciao
    Il guaio per i poveri computers e' che sono gli uomini a comandarli.

    Attenzione ai titoli delle discussioni: (ri)leggete il regolamento
    Consultate la discussione in rilievo: script / discussioni utili
    Usate la funzione di Ricerca del Forum

  3. #3
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    prima di valutare la tempistica,
    fai tutte richieste get senza impedire di ripescarle dalla cache: non puoi avere risultati affidabili
    http://forum.html.it/forum/showthrea...readid=1340072

  4. #4

    Re: domanda su javascript (query instabile)

    [QUOTE]Originariamente inviato da net4fun
    Salve a tutti,
    Vengo subito al problema. Questa query non è "sicura"
    codice:
    mysql_query("SELECT COUNT(*) AS count FROM RegistrazioneSchedine WHERE data= '$data' AND Nickname= '$_GET[username]'");
    mi restituisce a volte valori non veri, ho notato che a volte inizia con zero ed è impossibile perchè quando viene effettuata c'è già almeno una registrazione, poi a volte è un valore in meno di quelli realmente registrati, però alla registrazione successiva ritorna alla normalità facendo salti (dipende quanti valori non ha contato precedentemente)

    Questo è il codice:
    codice:
     			 
    function doWork(){    
    				httpObject = getHTTPObject();
    				if (httpObject != null) {
    					httpObject.open("GET", "components/com_jumi/files/elabora.php?username="
    									+document.getElementById('username').value+"&numero1="
    									+document.getElementById('numero1').value+"&numero2="
    									+document.getElementById('numero2').value+"&numero3="
    									+document.getElementById('numero3').value+"&numero4="
    									+document.getElementById('numero4').value+"&numero5="
    									+document.getElementById('numero5').value, true);
    					httpObject.send(null); 
    					httpObject.onreadystatechange = setOutput;
    				}
    					} 
    			    
    				httpObject = getHTTPObject();
    
    function setOutput2(){
    					if(httpObject.readyState == 4){
    						document.getElementById('ns').value = httpObject.responseText;
    						}
    				
    				if (httpObject != null) {
    					httpObject.open("GET", "components/com_jumi/files/ns.php?username="
    									+document.getElementById('username').value, true);
    					httpObject.send(null); 
    					httpObject.onreadystatechange = setOutput2;
    					
    				}				
    			}//fine doWork
    ns.php
    Codice PHP:
    <?php 
        $db_host 
    "****"
        
    $db_user "****"
        
    $db_password "****"
        
    $db_name "****";  
                         
                
    $db mysql_connect($db_host$db_user$db_password);
                      
    mysql_select_db($db_name$db);

                
    $data=date("d-m-Y");
            
            
    $query mysql_query("SELECT COUNT(*) AS count FROM RegistrazioneSchedine WHERE data= '$data' AND Nickname= '$_GET[username]'");     
            
    $sql mysql_fetch_array($query);  
            
    $_GET['ns'] = $sql['count'];
                        
        
    mysql_close($db); 
               
        if (isset(
    $_GET['ns']))
            echo 
    $_GET['ns'];
       
     
    ?>
    elabora.php
    Codice PHP:
    <?php 


        $db_host 
    "****"
        
    $db_user "*****"
        
    $db_password "****"
        
    $db_name "*****";                   
                
    $db mysql_connect($db_host$db_user$db_password);
                      
    mysql_select_db($db_name$db);

                
    $data=date("d-m-Y"); $ora=date("H:i:s");

            
    mysql_query("INSERT INTO RegistrazioneSchedine(Nickname, numero1, numero2, numero3, numero4, numero5, data, ora) 
                         VALUE ('
    $_GET[username]', '$_GET[numero1]', '$_GET[numero2]', '$_GET[numero3]', '$_GET[numero4]', '$_GET[numero5]', '$data', '$ora')");
                        
        
    mysql_close($db); 
               
        if (isset(
    $_GET['numero1']))
            echo 
    '';    

        if (isset(
    $_GET['numero2']))
            echo 
    '';    

        if (isset(
    $_GET['numero3']))
            echo 
    '';    

        if (isset(
    $_GET['numero4']))
            echo 
    '';    
            
        if (isset(
    $_GET['numero5']))
            echo 
    '';    
        
    ?>

    in poche parole dovrei fare in modo che sia sicuro che la query INSERT INTO... sia stata completata per poi fare la SELECT COUNT...

    ringrazio anticipatamente chi mi risponderà

  5. #5
    Originariamente inviato da Xinod
    prima di valutare la tempistica,
    fai tutte richieste get senza impedire di ripescarle dalla cache: non puoi avere risultati affidabili
    http://forum.html.it/forum/showthrea...readid=1340072
    come posso ovviare al problema? magari effettuando chiamate POST?

    aggiungo..

    mi sorge un dubbio:
    come fa a trovarsi a zero, se è praticamente impossibile perchè la query COUNT viene effettuata dopo il primo INSERT? Rimango nella convinzione che è un problema di velocità delle due query in attesa di una smentita

  6. #6
    Ho fatto queste modifiche ma non va ugualmente..dove sbaglio??

    codice:
    httpObject.open("GET", "components/com_jumi/files/elabora.php?cachebuster="+new Date().getTime()+"$username="
    									+document.getElementById('username').value+"&numero1="
    									+document.getElementById('numero1').value+"&numero2="
    									+document.getElementById('numero2').value+"&numero3="
    									+document.getElementById('numero3').value+"&numero4="
    									+document.getElementById('numero4').value+"&numero5="
    									+document.getElementById('numero5').value, true);

    codice:
    httpObject.open("GET", "components/com_jumi/files/ns.php?cachebuster="+new Date().getTime()+"$username="
    									+document.getElementById('username').value, true);

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.