Jump to content

Php Update Running Twice


wayneambler

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.