wayneambler Posted November 22, 2012 Share Posted November 22, 2012 Hi, I'm developing a website with a simple page views counter running php & mysql. Here is the code that I have: <?php mysql_query("UPDATE blogs SET views = views + 1 WHERE id = $post"); ?> This is working correctly when I visit the page as the count increases by 1, but if I refresh the page it doubles and if I leave the page it runs again. It's like it is running when I enter the pade and then running again when I leave it. I know there must be a simple solution or a minor error that I have made somewhere but I can't figure out what it is. Can someone please help me with this. Thank you in advance Quote Link to comment https://forums.phpfreaks.com/topic/271044-php-update-running-twice/ Share on other sites More sharing options...
Beeeeney Posted November 22, 2012 Share Posted November 22, 2012 Does your URL change in your browser once you send the information to the database? There's probably a command in the URL making it update when you refresh. That doesn't sound like it makes sense, reading it back. I'll show an example: When you update, does your site change from "www.website.com/index.php" to something like "www.website.com/index.php?updatedb=true" or something similar? Basically, does your URL change? If it does then you're refreshing that second URL and asking it to update the database. Quote Link to comment https://forums.phpfreaks.com/topic/271044-php-update-running-twice/#findComment-1394457 Share on other sites More sharing options...
wayneambler Posted November 22, 2012 Author Share Posted November 22, 2012 Hi Beeeeney, Thanks for the response. The url stays exactly the same every time I visit I basically go from http://blog.local.co.uk/blogs.php to http://blog.local.co.uk/blog.php?p=1&t=title-for-the-blog which works, but then when I leave back to the home page it seems to run again so when I go back to the blog it has gone up by 2, i'e. the first time I visit it goes to 1 view and if I leave and come back again it has 3 views. Quote Link to comment https://forums.phpfreaks.com/topic/271044-php-update-running-twice/#findComment-1394462 Share on other sites More sharing options...
Beeeeney Posted November 22, 2012 Share Posted November 22, 2012 Post your code! Quote Link to comment https://forums.phpfreaks.com/topic/271044-php-update-running-twice/#findComment-1394464 Share on other sites More sharing options...
Pikachu2000 Posted November 22, 2012 Share Posted November 22, 2012 Well, of course it does if you're running the UPDATE query unconditionally. Post your code so there's no need to guess what's happening. Quote Link to comment https://forums.phpfreaks.com/topic/271044-php-update-running-twice/#findComment-1394465 Share on other sites More sharing options...
wayneambler Posted November 22, 2012 Author Share Posted November 22, 2012 I've posted the code: <?php mysql_query("UPDATE blogs SET views = views + 1 WHERE id = $post"); ?> I'm updating the views column in my blog table where the id is the same as p in the URL (http://blog.local.co.uk/blog.php?p=1) Quote Link to comment https://forums.phpfreaks.com/topic/271044-php-update-running-twice/#findComment-1394466 Share on other sites More sharing options...
Beeeeney Posted November 22, 2012 Share Posted November 22, 2012 (edited) Your full code, man. Use tags. Edited November 22, 2012 by Beeeeney Quote Link to comment https://forums.phpfreaks.com/topic/271044-php-update-running-twice/#findComment-1394471 Share on other sites More sharing options...
wayneambler Posted November 22, 2012 Author Share Posted November 22, 2012 <?php $page = 'blog' ?> <?php include("include/site-php.php"); ?> <?php $post = $_GET["p"]; ?> <?php mysql_query("UPDATE blogs SET views = views + 1 WHERE id = $post"); ?> <?php $get_blogs_sql = mysql_query("SELECT * FROM blogs WHERE id = $post") or die(mysql_error()); while ($blogs = mysql_fetch_array($get_blogs_sql)) { $blog_id = $blogs['id']; $blog_title = $blogs['title']; $blog_photo = $blogs['photo']; $blog_photographer = $blogs['photographer']; $blog_photo_url = $blogs['photographer_url']; $blog_blog = nl2br(substr($blogs['blog'], 0,600)); $blog_cat_name = $blogs['cat_name']; $blog_cat_type = $blogs['cat_type']; $blog_date = $blogs['date']; $blog_views = $blogs['views']; $blog_date = date("l dS M Y",strtotime($blog_date)); $blog_title_string = str_replace(" ","-",$blog_title); $blog_title_string = strtolower($blog_title_string); if ($blog_photographer == '') { $blog_photographer_info = ""; } else { $blog_photographer_info = "<div class=\"photoBy\">Photo by: <a href=\"$blog_photo_url\" target=\"_blank\">$blog_photographer</a></div>"; }; $display_blog .= " <div class=\"blogSection\"> <div class=\"blogTop\"></div> <div class=\"blogMiddle\"> <div class=\"blog\"> <div class=\"blogTitle\"><h1><a href=\"$site_url/blog.php?p=$blog_id&t=$blog_title_string\">$blog_title</a></h1></div> <div class=\"blogDetails\"> <div class=\"blogInfo blogDate\">$blog_date</div> <div class=\"blogInfo blogCategory\">Posted In <a href=\"blogs.php?category=$blog_cat_type\">$blog_cat_name</a></div> <div class=\"blogInfo blogViews\">$blog_views Views</div> <div class=\"clearLeft\"></div> </div> <div class=\"blogPhoto\"> <img src=\"images/$blog_photo\"/> $blog_photographer_info </div> <div class=\"blogContents\">$blog_blog</div> </div> </div> <div class=\"blogBottom\"></div> </div> "; } ?> <?php $get_about_sql = mysql_query("SELECT * FROM blog_about") or die(mysql_error()); while ($about = mysql_fetch_array($get_about_sql)) { $blog_about = $about['about']; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <?php include("include/head.php"); ?> <!--Style--> <link rel="stylesheet" type="text/css" href="css/blogs.css" /> </head> <body> <a id="top" name="top"></a> <?php include "include/header.php"; ?> <div id="pageHolder"> <div id="pageInner"> <div id="mainColumn"> <div id="mainContentHolder"> <?php echo $display_blog; ?> <div class="blogSection"> <div class="blogTop"></div> <div class="blogMiddle"> <div id="subscribe"> <div class="postSubscribe">Did You Enjoy This Post? Please Subscribe To My Blog <a href="<?php echo $site_url; ?>/feed/">Here!</a></div> <div class="Shareaholic"><div class="shr_class shareaholic-show-on-load"></div></div> </div> </div> <div class="blogBottom"></div> </div> <div class="blogSection"> <div class="blogTop"></div> <div class="blogMiddle"> <div id="about"> <h1>About Me</h1> <div class="blogAbout"> <?php echo $blog_about; ?> </div> </div> </div> <div class="blogBottom"></div> </div> <div class="blogSection"> <div class="blogTop"></div> <div class="blogMiddle"> <div id="comment"> <div id="disqus_thread"></div> <script type="text/javascript"> /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */ var disqus_shortname = 'wayneambler'; // required: replace example with your forum shortname /* * * DON'T EDIT BELOW THIS LINE * * */ (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })(); </script> <noscript>Please enable Javascript to view the <a href="http://disqus.com/?ref_noscript" target="_blank">comments powered by Disqus.</a></noscript> <a href="http://disqus.com" class="dsq-brlink" target="_blank">comments powered by <span class="logo-disqus">Disqus</span></a> </div> </div> <div class="blogBottom"></div> </div> </div> </div> <?php include("include/sidebar.php"); ?> <div class="clearLeft"></div> </div> <?php include("include/row-top.php"); ?> </div> <?php include("include/pre-footer.php"); ?> <?php include("include/footer.php"); ?> <?php include("include/post-footer.php"); ?> <?php include("include/analytics.php"); ?> <?php include("include/scripts.php"); ?> <!-- Start Shareaholic Sexy Bookmark settings --> <script type="text/javascript"> var SHRSB_Settings = {"shr_class":{"src":"shareaholic/","link":"","service":"5,7,88,304,309,2,38,201","apikey":"0747414f47c2b684cf5480c36b2478689","localize":true,"shortener":"bitly","shortener_key":"","designer_toolTips":true,"tip_bg_color":"black","tip_text_color":"white","twitter_template":"${title} - ${short_link} via @Shareaholic"}}; var SHRSB_Globals = {"perfoption":"1"}; </script> <!-- End Shareaholic Sexy Bookmark settings --> <!-- Start Shareaholic Sexy Bookmark script --> <script type="text/javascript"> (function() { var sb = document.createElement("script"); sb.type = "text/javascript";sb.async = true; sb.src = ("https:" == document.location.protocol ? "https://dtym7iokkjlif.cloudfront.net" : "http://cdn.shareaholic.com") + "/media/js/jquery.shareaholic-publishers-sb.min.js"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(sb, s); })(); </script> <!-- End Shareaholic Sexy Bookmark script --> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/271044-php-update-running-twice/#findComment-1394476 Share on other sites More sharing options...
wayneambler Posted November 22, 2012 Author Share Posted November 22, 2012 **RESOLVED** I just found out that it's the Disqus Javascript code that is effecting the MySQL Update script - Does anyone have any ideas wy this might be??? Quote Link to comment https://forums.phpfreaks.com/topic/271044-php-update-running-twice/#findComment-1394485 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.