homepage Welcome to WebmasterWorld Guest from 174.129.80.166
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP query return old (overwriten) data from DB !
punisa




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

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.

 

Alcoholico




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

It looks like a browser cache issue:
Try adding these headers to your php script:
<?php
$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.

punisa




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

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.

astupidname




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

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.....

punisa




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

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
alert(feedbackData);
}


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

if(isset($_POST['currentImage'])){
$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?

punisa




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved