Question

I have one webpage (lets call it A.html). A.html has some javascript which switches the page to a dynamic php page (lets call it B.php), using a simple document.location="B.php". B.php runs a mysql query based on the cookies it sees, and returns a result based on the mysqli->affected_rows variable. However, everytime the page switches over mysqli->affected_rows is equal to 0, even though the mysql table does get changed the way it's supposed to. Interestingly, when I go straight to B.php using the url bar (or refresh the page) then mysqli->affected_rows is 1, like it's supposed to be.

Looking at wireshark, the only difference between the two GET requests is that the second (non-javascript) one has a line with Cache-Control: max-age=0. Does anyone know why this would affect anything, and if there's a way I can fix this?

EDIT: Heres' the code

    $req = $mysqli->prepare('update users set sts=NOW() where i=? and sid=? and sip=? and NOW()-sts <= '.$authentication_timeout.';');
    if ($mysqli->error) {
        log_mysql_error($mysqli);
        die('Unexpected error:'.$mysqli->error);
    }
    $req->bind_param('sss',$uid,$sid,$_SERVER['REMOTE_ADDR']);
    $req->execute();

    print $mysqli->affected_rows;
    $req->close();
Was it helpful?

Solution 3

Wasn't an issue with the cache at all. The problem arises because in this particular case the same row is being updated just before this code is executed, and there isn't enough time delay for NOW() to be different. So while the row matches, there isn't a change in the data so $mysqli->affected_rows is still equal to zero. $mysqli->info shows that "Rows matched: 1, Rows changed: 0".

OTHER TIPS

If it's indeed the cache souring your brew, then I guess doing

document.location="B.php?nocache=" + new Date().getTime();

will solve the problem. Can you check and verify if that's it?

You're not showing any code so I can only guess, but affected_rows() will return NULL on an update that doesn't change anything in the table. So presumably, if you reload your page, the second load will show 0 even if the update ran through on the page before.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top