bramabull100 Posted January 24, 2011 Share Posted January 24, 2011 Hope this is clear and concise! I have a view article page and every time you land on the page then I have a query that runs an update using sql. Now the problem is everytime the script is run it counts one hit as 2. For example if an article has been viewed 4 times. Next time you land on the page it shows the page has been seen 6 times instead of 5. Here is my code below for the view_article page: <body bgcolor="#000000"> <div id="site_div" align="center"> <div id="wrapper_div"> <!-- header --> <?php include 'header.php'; ?> <!-- header --> <div id="main_nav"> <div id="link1_div"><p class="link1_class"><a href="index.html" class="link1_class">All Articles</a></p></div> <div id="link2_div"><p class="link2_class"><a href="index.html" class="link2_class">Reviews</a></p></div> <div id="link3_div"><p class="link2_class"><a href="index.html" class="link2_class">Previews</a></p></div> <div id="link4_div"><p class="link2_class"><a href="buys.php?lightbox[width]=700&lightbox[height]=400&lightbox[modal]=true" class="link2_class lightbox">Videos</a></p></div> <div id="link5_div"><p class="link2_class"><a href="rents.php?lightbox[width]=700&lightbox[height]=400&lightbox[modal]=true" class="link2_class lightbox">News</a></p></div> <div id="link6_div"><p class="link2_class"><a href="index.html" class="link2_class">Podcasts</a></p></div> <div id="link7_div"><p class="link2_class"><a href="index.html" class="link2_class">Contact Us</a></p></div> <div id="link8_div"><p class="link2_class"><a href="index.html" class="link2_class"></a></p></div> </div> <div id="sub_nav"> <div id="sub_nav_div"> <p class="filter_by_class">FILTER BY: <a href="index.php" style="color:#21a8f7; text-decoration:underline">All</a> <a href="xbox360.php">Xbox 360</a> <a href="#">PS3</a> <a href="#">Wii</a> <a href="#">PC</a> <a href="#">PSP</a> <a href="#">DS</a><a href="#">Mobile</a><a href="#">Older Platforms</a></p></div> </div> </div> <div id="content_div"> <div id="content_spacer"> </div> <!-- TRACK PAGE <span class="posthilit">VIEWS</span> --> <?php //DATABASE CONNECTION include_once 'dbc.php'; $id = $_GET['id']; mysql_query("UPDATE articles SET `article_views` = `article_views`+1 WHERE idarticles='$id'"); ?> // TrACK PAGE <span class="posthilit">VIEWS</span> // MAIN CONTENT if(isset($_GET['id']) && is_numeric($_GET['id']) ) { $query = "SELECT * FROM articles WHERE idarticles= {$_GET['id']}"; if($r = mysql_query($query)) { $row = mysql_fetch_array($r); echo " <div id=\"main_article_div\"> <div id=\"top_blue_content_div\"> {$row['article_type']}</div> <div id=\"image_main_content_div\"><img src=\"{$row['article_big_image']}\" width=\"200\" height=\"112\" alt=\"main_article_image\" /></div> <div id=\"content_area\"><p class=\"main_content_title_class\">{$row['article_title']} <div id=\"addthisspacer\"> <!-- AddThis Button BEGIN --> <div class=\"addthis_toolbox addthis_default_style \"> <a class=\"addthis_button_facebook_like\" fb:like:layout=\"button_count\"></a> <a class=\"addthis_button_tweet\"></a> <a class=\"addthis_counter addthis_pill_style\"></a> </div> <script type=\"text/javascript\" src=\"http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4d1d4fb34dc87309\"></script> <!-- AddThis Button END --> </div> </p> </div> </div> "; } } ?> <!-- MAIN CONTENT ENDS --> </div> <div id="lower_content_div"> <!-- THIS IS WHERE THE MAIN ARTICLE/COMMENTS STARTS --> <?php //include 'dbc.php'; if(isset($_GET['id']) && is_numeric($_GET['id']) ) { $query = "SELECT * FROM articles WHERE idarticles= {$_GET['id']}"; if($r = mysql_query($query)) { $row = mysql_fetch_array($r); echo " <div id=\"left_lower_content\" class=\"break-word\"> {$row['actual_article']} </div> "; } } ?> <!-- THIS IS THE END OF THE MAIN ARTICLE/COMMENTS QUERY --> </div> <div id="footer_div"> <div id="footer_1"></div> <div id="actual_footer"> <div id="top_footer_div"> <div id="top_footer_div_left"><a href="login.php?lightbox[width]=396&lightbox[height]=200&lightbox[modal]=true"><img src="images/rent-or-buy-2-logo_small2.png" width="260" height="39" alt="small_logo" class="small_logo_class" /></a></div> <div id="top_footer_div_right"> <div id="login_register_div_2"> <p class="logintext"><a href=""><img src="images/rss_icon.png" width="16" height="16" alt="rss_icon" /></a></p><p class="logintext"><a href="http://www.twitter.com"><img src="images/twitter_icon.png" width="16" height="16" alt="twitter_icon" /></a></p><p class="logintext"><a href="http://www.youtube.com"><img src="images/youtube_icon.png" width="38" height="16" alt="youtube_icon" /></a></p><p class="logintext"><a href="http://www.facebook.com"><img src="images/facebook_icon.png" width="16" height="16" alt="facebook_icon" /></a></p> </div> </div> <div id="search_div2"> <form action="#" method="post" name="search"> <input type="text" value="Search!" class="formimage" /> <INPUT TYPE="IMAGE" src="images/search_bar_button.jpg" BORDER="0" style="padding:0" class="formbutton"> </form> </div> </div> <div id="actual_footer_bottom_section"> <div id="actual_footer_bottom_section_top"></div> <div id="actual_footer_bottom_section_top_links"><a href="index.php">All Articles</a> | <a href="#">Reviews</a> | <a href="#">Previews</a> | <a href="#">"Buys"</a> | <a href="#">"Rents"</a> | <a href="#">Videos</a> | <a href="#">News</a> | <a href="#">Games</a> </div> <div id="actual_footer_bottom_section_bottom"> <p class="actual_footer_bottom_section_paragraph">Design By: Felix Henriques | Created By: Felix Henriques | Copyright 2010 Buy Or Rent Games</p></div> <div id="actual_footer_bottom_section_top"></div> </div> </div> </div> </div> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
Maq Posted January 24, 2011 Share Posted January 24, 2011 (I put tags around your code, please use them next time [your format is really spaced out BTW]) Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 24, 2011 Share Posted January 24, 2011 First of all, why are you posting the entire page and why did you not enclose it in CODE tags [Thanks Maq]? Second, what debugging have you done to narrow down the problem and/or rule out certaint hings and what were the results? I have no problem helping people, but it is nice to see that they have at least put forth some effort. There are several include files in that script so how do you know none of them are creating the duplicate or that you don't have a recursiv loop some where? The first thing I would do is change that UPDATE query to mysql_query("UPDATE articles SET `article_views` = `article_views`+ 0 WHERE idarticles='$id'"); Then if the counter goes up by 1 then you kow the problem is some other code is incrementing the counter. If it doesn't then you know the UPDATE query is being run twice. SInce it doesn't appear to be in a loop I would guess the page is being called twice. I do notice that you seem to be calling the exact same SELECT query on the page: $query = "SELECT * FROM articles WHERE idarticles= {$_GET['id']}"; There should be no reason for that. I woudn't think that has anything to do with this specific problem, but may indicate that the logic is to blame. Quote Link to comment 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.