computermax2328 Posted August 30, 2012 Share Posted August 30, 2012 Hello Again, In the database of the blog I am building I have a column for each article called "count." What I did on the article page is every time the id for the article is called to the page to select all of the content for that specific article the count will be selected from the database and updated plus one. The problem that I am having is that it is adding two to the count every time. The code looks something like this. <?php require("../includes/connection.php"); $id = $_GET['id']; $query = "SELECT * FROM blog WHERE id=$id"; $select = mysql_query($query); while ($row = mysql_fetch_array($select)) { $id = $row['id']; $count = $row['count']; $date = $row['Date']; $title = $row['Title']; $sub = $row['Subtitle']; $author = $row['Author']; $content = $row['Content']; $tags = $row['Tags']; } $addcount = $count+1; $update = "UPDATE blog SET count=$addcount WHERE id=$id"; $upcount = mysql_query($update); ?> Any ideas?? Quote Link to comment https://forums.phpfreaks.com/topic/267782-php-article-counter/ Share on other sites More sharing options...
spiderwell Posted August 30, 2012 Share Posted August 30, 2012 it looks like it should work! how about echonig out the count before you increment it and also echoing out the sql statement to help debug that way? Quote Link to comment https://forums.phpfreaks.com/topic/267782-php-article-counter/#findComment-1373782 Share on other sites More sharing options...
MMDE Posted August 30, 2012 Share Posted August 30, 2012 First off, make sure you sanitize that $_GET['id'] input before you use it in a query with MySQL. Use regex for example to check if it's a number. if(!preg_match('/[^0-9]/', $_GET['id'])){ // safe to use $_GET['id'] } The second part is that you don't need to search for it first, you only need to update. "UPDATE blog SET count=count+1 WHERE id=$id"; I guess you need to search anyways, because you need the content of the post. Do this with all of your mysql_querys: mysql_query($query) or die(mysql_error()); $upcount should in your code tell you if you if the MySQL update was successful, with either a TRUE/FALSE value. Don't mistake it for the actual counter. Quote Link to comment https://forums.phpfreaks.com/topic/267782-php-article-counter/#findComment-1373783 Share on other sites More sharing options...
PFMaBiSmAd Posted August 30, 2012 Share Posted August 30, 2012 Your page is probably being requested twice, either by the browser (different browser do this for different reasons), by something you are doing in your code, by url rewriting, or even how your web host is sending http requests to your web server (assuming this is occurring on a live web site.) Check your web server's access log to see if there are closely spaced http requests from the same ip address with the same id on the end of the url. If you are getting multiple requests and they are not due to a coding error on your part, the ultimate solution is to NOT maintain a simple count, but to store a record in a database table that records, at a minimum, the article id, the ip address of the http request, and the datatime of the http request. You will then be able to use this information to filter out and ignore duplicate http requests to get a more accurate count. Another method to detect and prevent duplicate requests is to set session variables with the article id and the datatime of the http request and if another request occurs for the same article id within a short time (~ 1 second), ignore it. Quote Link to comment https://forums.phpfreaks.com/topic/267782-php-article-counter/#findComment-1373883 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.