<script>
<!--
var milisec=0
var seconds=0
document.d.d2.value='0'
function display(){
if (milisec>=9){
milisec=0
seconds+=1
}
else
milisec+=1
document.d.d2.value=seconds+'.'+milisec
setTimeout("display()",100)
}
display();
setInterval(function(){
$.post("procesare.php",{score: seconds},function(data){
$('#text').html(data);
});
},1000);
//-->
</script>
This is my JS code. I'm trying to store in database the time a user is on my index. Here is my "procesare.php" where I call the queries:
if(isset($_POST['score'])){
$result = mysql_query("SELECT nickname, score FROM users ORDER BY score DESC");
$rank = 1;
if (mysql_num_rows($result)) {
while ($row = mysql_fetch_assoc($result)) {
echo "<br />{$rank}.
{$row['nickname']} -->
{$row['score']}";
$rank++;
}
}
$qry="SELECT score FROM users WHERE username='{$_SESSION['user']}'";
$result = mysql_query($qry) or die(mysql_error());
$result=mysql_fetch_row($result);
$total = $_POST['score'] + $result[0];
$qry="UPDATE users SET score='$total' WHERE username='{$_SESSION['user']}'";
mysql_query($qry) or die(mysql_error());
My problem is that the JS timer works normally, second by second, but in the database, it is a chaos. The time increases randomly..... I cant figure out why ....
Thanks in advance!