Jump to content

+1 in update query not working properly


xploited

Recommended Posts

I am having a bit of a problem with an update query that is at the top of a product info page. I'm trying to add 1 to the 'count_views' column every time the page is loaded. Unfortunately, it doesn't always add 1. I've seen it add up to 3 at a time. How is this possible? On a second page load (when the view count is at 1) it can bump up to 3 randomly. Where's it getting the extra view? Here's a portion of the code:

 

$product_query = mysql_query("select * from products where id = '".(int)$id."'");
$product = mysql_fetch_array($product_query);
$add_view = mysql_query("update products set count_views = count_views +1 where id = '" . (int)$id . "'");

 

I'm not quite sure what's causing it, but this is new to me. I've never seen this happen anywhere else. And any changes I seem to make to it do nothing to help the situation. There's no additional calls to $add_view after this bit. Any help would be appreciated.

Link to comment
Share on other sites

So, which browser are you using? FF has a nasty habit of requesting pages twice due to 3-4 different things. IE can also request default documents twice while just trying to get a favicon file. Is this a default docoment like index.php? Does this only happen on one page or on any page with that code? Have you tried it with a different browser? How exactly is the page being requested, a link, a form submission? If it is a form, what is your form code because if javascript is submitting the page and not preventing the browser's normal form handling from submitting the page you will get a double page request. Are you doing any URL rewriting, which can cause pages to be requested twice if the rewrite rules are not correct? Do you have code on the current page or any page involved with that page, such as an include or a redirect that could cause the code to be executed more than once?

 

Basically, you need to provide the supporting information about what you are doing that is relevant to how the page with that code is being requested that would let someone help you.

Link to comment
Share on other sites

- I do use URL rewriting. Here it is below, it is the only ^product line of its type in the htaccess file:

 

RewriteRule ^product/([0-9]+)/([^/]+)$ /view-product.php?id=$1&type=2 [NC]

 

- the page is requested via links and is a product page (not homepage)

- this occurs on different browsers and os

- from what i can tell, nothing in the code would cause it to execute twice

 

However, I have cut those potential routes off by implementing an if statement that checks if the session has seen the product yet. So I would think that would stop a second reload from causing an increment in the value. However, it doesn't. You can see this code here:

 

if(!$_SESSION['products_viewed'][$product['id']]){
  $add_view = mysql_query("update products set count_views = count_views +1 where id = '" . (int)$id . "'");
  $_SESSION['products_viewed'][$product['id']] = $product['id'];
}

 

I can also confirm that if statement works correctly, because reloading the page within that session will never cause additional increments of the count_views.  Yet, the random +2 or +3 happens still on the first page load.

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.