0
<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!

1 Answer 1

1

I don't know what you're ultimately trying to do, but it seems a bit fishy.

I'm also not clear on what 'chaos in database' means. For what it's worth, you probably should update like so:

$qry="UPDATE users SET score = score + '$_POST['score']' WHERE username='{$_SESSION['user']}'";

You don't need the separate query for looking up existing score. Just increment it. You should also watch out for sql injection.

Sign up to request clarification or add additional context in comments.

3 Comments

Im trying to make a website, in which every user get rewarded with 1 score/ seocnd on page. Hoping that people will get competitive and enetr on my site. Thanks for the answer, ill try and let u know if it worked
I finally managed to fix it. I had a logical error. Every second i was adding my current time, not a second per second . if u get me. Thanks a lot anyways:D
So if I open my browser console and type: $.post("procesare.php",{score: 1000000}); I'll quickly be at the top of the leaderboard? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.