Update problem mysql

Discussion in 'MySQL' started by kusal, Jun 9, 2009.

  1. #1
    $views++;
    
    echo $views; //2
    	
    mysql_query("update vehicles set hits = '$views' where id = '$id' limit 1");
    
    echo $views; //2
    
    Code (markup):
    This is the problem, lets say my previous views are 1. after $view++ it should be 2 but after the update query tables is updated with 3

    I cannot really find why this happening, why mysql add extra 1
     
    kusal, Jun 9, 2009 IP
  2. sssharlasss

    sssharlasss Peon

    Messages:
    17
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It doesn't look like there are any problems with what you have right there. I would suggest going through your code with a fine-tooth comb.

    A possibility might be that auto-increment is on for the "hits" column, or something weird like that.
     
    sssharlasss, Jun 9, 2009 IP
  3. nirajkum

    nirajkum Active Member

    Messages:
    815
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #3
    make you you are executing this statement twice ...
     
    nirajkum, Jun 9, 2009 IP
  4. kusal

    kusal Peon

    Messages:
    91
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you for the replies

    problem was that, this page was requested 2 times before actually loading the page

    I don't know why but after I removed a table from my page it worked find. page was only requested one time.
     
    kusal, Jun 9, 2009 IP
  5. optimeramera

    optimeramera Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    An easier way to increase the counter field in the database would be:

    
    mysql_query("update vehicles set hits = hits+1 where id = '$id' limit 1");
    
    Code (markup):
    Then you wouldn't need to involve the $views variable from php. Although if you're fetching it from the database anyway to display it on the page then you might as well keep the code as it is.
     
    optimeramera, Jun 10, 2009 IP
  6. makaleus

    makaleus Peon

    Messages:
    264
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    and
    use it
     
    makaleus, Jun 11, 2009 IP
  7. kalpana001001

    kalpana001001 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This code repeted twice thats the problem..
     
    kalpana001001, Jun 12, 2009 IP