Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP query return old (overwriten) data from DB !



6:19 pm on Nov 20, 2010 (gmt 0)

5+ Year Member

This is the strangest problem I have encountered so far.

I update some data using AJAX and store a certain number in database.

This is the processes:
0) current number in my DB table is "4"
1) I arrive on page and AJAX saves number "5" in my DB table (using UPDATE sql query)
2) I double check my PHPMYADMIN database and make sure that the number is stored - it is. The number has been set to "5"
3) I navigate to the next page and make a query to return the number from DB - it return "4" (!)

As if somehow the query returns the number that was previously in the DB table, but has since been replaced with "5".
How is this even possible?

It keeps returning the old overwritten number until I refresh the page. Then it returns "5", as it should.

What could be causing this? I tried searching the web but could not find any similar situation.


7:59 pm on Nov 20, 2010 (gmt 0)

5+ Year Member

It looks like a browser cache issue:
Try adding these headers to your php script:
$expires = gmdate('D, d M Y H:i:s', time()-(60*60*24*365*2)) . ' GMT';
header('Expires: '.$expires, true);
header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache'); // HTTP/1.0

Add them before ANY other stuff that echoes anything to the browser.


8:12 pm on Nov 20, 2010 (gmt 0)

5+ Year Member

Alcoholico, thank you for your suggestion. I tried doing that, but unfortunately it did not help.

I thought that the problem might be browser related too.
I even tried it with IE, Firefox and Opera - all give the same problem.


8:38 am on Nov 21, 2010 (gmt 0)

5+ Year Member

3) I navigate to the next page and make a query to return the number from DB - it return "4" (!)

In what manner are you making the query? Using ajax? If so, that could be where the caching issue is. In addition to using headers in an ajax call I also set a variable: nocacheID = "preventCacheTimestamp="+ new Date().getTime() and then append that to the params (if POST) or url (if GET) to ensure the ajax call retrieves fresh non-cached response from server. If that's not your issue, perhaps you are doing something else with sessions or other cookies which interferes? May need to actually show some code.....


3:28 pm on Nov 21, 2010 (gmt 0)

5+ Year Member

javascript (jquery) code inside my main page

currentImage = 5;
new Ajax.Request("/operations.php",{method:'post',postBody:'currentImage='+currentImage,onComplete:currentImageSaved});

function currentImageSaved(req){
feedbackData = req.responseText;
//check what happened at operations.php, shows echoed data from the code at operations.php

operations.php document (where data gets written to DB)

$currentImage= mysql_real_escape_string($_POST['currentImage']);

$sql = "UPDATE imageGallery SET currentImage='".$currentImage."' WHERE user ='1' LIMIT 1";
$result = mysql_query($sql, $conn) or die(mysql_error());

$sql = "SELECT * FROM imageGallery WHERE user= '1' LIMIT 1";
$result = mysql_query($sql, $conn);
if (mysql_num_rows($result) == 0) {
} else {while ($row = mysql_fetch_array($result)) {
echo $row['currentImage'];


This should update the table "imageGallery " with a new value of "5".
I open my phpmyadmin sql DB in new tab and check weather the data was indeed updated.
Yes it is. Before was "4", now its "5".

Then I navigate to the next link in my main document and at the begining I have this PHP code:

$sql = "SELECT * FROM imageGallery WHERE user = '1' LIMIT 1";
$result = mysql_query($sql, $conn);
if (mysql_num_rows($result) == 0) {
} else {while ($row = mysql_fetch_array($result)) {
echo $row['currentImage'];

It outputs the old value of "4" ! :(
If I keep clicking next and previous pages it keeps showing me "4", until I click refresh on my browser - then it finally outputs "5".

BTW, for testing purposes there is only one record in the DB table "imageGallery" and its the one where user field is set to "1", but that bit is not relevant here.

I'm completely puzzled.
If data was not being updated, I'd figure I have some error in my code.
But as I said before - data gets written down and I have proof that it does :)

I'm suspecting there might be a problem beyond my knowledge, perhaps something could be wrong with the database or the server?


4:44 pm on Nov 21, 2010 (gmt 0)

5+ Year Member

please ignore my problem, I have solved it.
Apparently there was a little bit too much being sent at the time.

I had an ajax call tied down to several onmousedown events, once I deleted those from code - everything works fine.
Not sure why it caused such a strange situation, but fortunately everything works fine now.
Guys thanks for reading and your suggestions anyway.

Featured Threads

Hot Threads This Week

Hot Threads This Month