NLCJ Posted July 26, 2010 Share Posted July 26, 2010 Hello, I know that the title might be a bit confusing, but I've got a problem. When I say that it has to do +1 it does +2. The code I use is located at the top of the page: $d_websitestatistics = mysql_fetch_array(mysql_query("SELECT * FROM websites WHERE id='".mysql_real_escape_string($_COOKIE['websiteid'])."'")); $views = $d_websitestatistics['views']; $newviews = $views+1; echo "UPDATE websites SET views='".$newviews."' WHERE id='".mysql_real_escape_string($_COOKIE['websiteid'])."'<br />".$d_websitestatistics['views']."<br />".$newviews; $newviews is $d_websitestatistics + 2, even though I said it should only add 1. What is the problem? Regards, NLCJ Link to comment https://forums.phpfreaks.com/topic/208932-1-2/ Share on other sites More sharing options...
Mchl Posted July 26, 2010 Share Posted July 26, 2010 Something simpler maybe? UPDATE websites SET views= views + 1 WHERE id= ... Link to comment https://forums.phpfreaks.com/topic/208932-1-2/#findComment-1091347 Share on other sites More sharing options...
NLCJ Posted July 26, 2010 Author Share Posted July 26, 2010 Hehe. Tried this before but I guess I made a typo when I did it. Thanks anyway! Link to comment https://forums.phpfreaks.com/topic/208932-1-2/#findComment-1091349 Share on other sites More sharing options...
NLCJ Posted July 26, 2010 Author Share Posted July 26, 2010 I think my server didn't pass its math test... It WORKED using: mysql_query("UPDATE websites SET views= views + 1 WHERE id='".mysql_real_escape_string($_COOKIE['websiteid'])."'"); But now it also adds 2 to the value... What is going on?! Regards, Link to comment https://forums.phpfreaks.com/topic/208932-1-2/#findComment-1091367 Share on other sites More sharing options...
Alex Posted July 26, 2010 Share Posted July 26, 2010 Are your sure you're not accidentally running multiple queries to update the same information? Link to comment https://forums.phpfreaks.com/topic/208932-1-2/#findComment-1091370 Share on other sites More sharing options...
NLCJ Posted July 26, 2010 Author Share Posted July 26, 2010 Triple checked it, nothing! Link to comment https://forums.phpfreaks.com/topic/208932-1-2/#findComment-1091371 Share on other sites More sharing options...
NLCJ Posted July 26, 2010 Author Share Posted July 26, 2010 This might be useful, I don't know. If I use: mysql_query("UPDATE websites SET views= views + 0.5 WHERE id='".mysql_real_escape_string($_COOKIE['websiteid'])."'"); it adds ALSO 2 to the value. And using: mysql_query("UPDATE websites SET views= views + 5 WHERE id='".mysql_real_escape_string($_COOKIE['websiteid'])."'"); results in adding 10 to the database. Double entries isn't a possibility since I only edited one line... I'm totally confused! :S Link to comment https://forums.phpfreaks.com/topic/208932-1-2/#findComment-1091377 Share on other sites More sharing options...
PFMaBiSmAd Posted July 26, 2010 Share Posted July 26, 2010 Your page is probably being requested twice by your browser or you are doing something else to cause that code to be included twice... Are you using firefox with any debugging add-on tools? Link to comment https://forums.phpfreaks.com/topic/208932-1-2/#findComment-1091391 Share on other sites More sharing options...
NLCJ Posted July 26, 2010 Author Share Posted July 26, 2010 I'm using Chrome, but do have FireFox installed. Link to comment https://forums.phpfreaks.com/topic/208932-1-2/#findComment-1091439 Share on other sites More sharing options...
NLCJ Posted July 26, 2010 Author Share Posted July 26, 2010 Thanks, I think that is the reason. I'm using FancyBox to load that page, the strange thing is that when a cookie exists it shouldn't update the value. If a cookie doesn't exist it will update the value, and directly set a cookie (this means that if the page reloads it won't update the values?). I'm going to Google about it! Thanks! Link to comment https://forums.phpfreaks.com/topic/208932-1-2/#findComment-1091444 Share on other sites More sharing options...
PFMaBiSmAd Posted July 26, 2010 Share Posted July 26, 2010 I have not heard of any cases where Chrome will request a page twice, but either your browser (due to debugging tools or to apply default character encoding), some javascript in your browser (typically some validation logic submitting a form and the actual form being submitted by the browser), your web server (if you are rewriting the url and/or some cases of an add on domain), or your logic on the server is causing this. Link to comment https://forums.phpfreaks.com/topic/208932-1-2/#findComment-1091445 Share on other sites More sharing options...
NLCJ Posted July 26, 2010 Author Share Posted July 26, 2010 (typically some validation logic submitting a form and the actual form being submitted by the browser) I thought this was the problem, since the form was submitting to that page and the javascript was opening that page in the FancyBox. Though the problem persists, even after removing the total 'action=' part... I've checked it, and the php file is only requested once in the whole script. Link to comment https://forums.phpfreaks.com/topic/208932-1-2/#findComment-1091453 Share on other sites More sharing options...
NLCJ Posted July 26, 2010 Author Share Posted July 26, 2010 I guess you were right, if I don't use FancyBox it is working. I have to search it in my JavaScript (oh, balls, just started with it yesterday). Thanks a lot. Link to comment https://forums.phpfreaks.com/topic/208932-1-2/#findComment-1091462 Share on other sites More sharing options...
PFMaBiSmAd Posted July 26, 2010 Share Posted July 26, 2010 You can typically use a session variable to prevent duplicate page requests. Set a session variable at the point where the first page request has successfully been processed and at the start of the page check if the session variable is already set and skip processing the page if it is set. Link to comment https://forums.phpfreaks.com/topic/208932-1-2/#findComment-1091463 Share on other sites More sharing options...
NLCJ Posted July 26, 2010 Author Share Posted July 26, 2010 You can typically use a session variable to prevent duplicate page requests. Set a session variable at the point where the first page request has successfully been processed and at the start of the page check if the session variable is already set and skip processing the page if it is set. I guess it still isn't the problem. If I've set it using sessions it shows a white page, and if you close the FancyBox and reopen it it shows the processed page. Even though it still adds 2 to the value! Strange... Link to comment https://forums.phpfreaks.com/topic/208932-1-2/#findComment-1091480 Share on other sites More sharing options...
NLCJ Posted July 26, 2010 Author Share Posted July 26, 2010 I give up... All solutions offered here will be tried (if someone feels like notifying me about another solution). How do I fix it? Simple: I created a HTML file which refreshes after 3 seconds to the other (PHP) file, then everything will be fine... Regards and thanks, NLCJ Link to comment https://forums.phpfreaks.com/topic/208932-1-2/#findComment-1091484 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.