Help: php mysql counter...

Discussion in 'PHP' started by Skillman13, Oct 27, 2009.

  1. #1
    I have a table (Gamepage) with the fields of 'id Description viewcount'

    The table has 10 records. Each record creates a dynamic php page...

    template.php?&id=1, template.php?&id=2, template.php?&id=3 etc (Based on the number in 'id' field -in Gamepage)

    I want a counter on each dynamic php page (so on the template)
    Which increases the value in the 'viewcount' field by one everytime it is viewed?

    Can anyone help me achieve this please?

    Thanks alot,

    James
     
    Skillman13, Oct 27, 2009 IP
  2. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Anyone? =/
     
    Skillman13, Oct 27, 2009 IP
  3. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #3
    show us table structure.

    its about using simple update query with id.
     
    mastermunj, Oct 27, 2009 IP
  4. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    How do i show you the structure? =/
     
    Skillman13, Oct 27, 2009 IP
  5. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    master? :( :p
     
    Skillman13, Oct 27, 2009 IP
  6. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #6
    ok... here is the update query..

    update Gamepage set viewcount = viewcount + 1;

    for table structure use following query

    show create table Gamepage;
     
    mastermunj, Oct 27, 2009 IP
  7. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I knew that top line, but how do i make it increase each record/dynamic page when it is viewed?

    -How does mysql know which record needs updating?
     
    Skillman13, Oct 27, 2009 IP
  8. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #8
    your url are template.php?id=1, template.php?id=2.

    id is already there in url... just get it from there and use it in query.

    
    $id = $_GET['id'];
    .......
    $update_query = "UPDATE Gamepage SET viewcount = viewcount + 1 where id = '$id'";
    
    PHP:
     
    mastermunj, Oct 27, 2009 IP
  9. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Are you sure that is right? I enter it into MySQL (to test)...

    (UPDATE Gamepage SET viewcount = viewcount + 1 WHERE id = '1') (-1 for example)

    And it returns...

    Affected rows: 0 (Query took 0.0002 sec)

    And 'viewcount' has not been affected =/

    Any thoughts?
     
    Skillman13, Oct 27, 2009 IP
  10. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Or if i just try to update it... (As a test)...

    UPDATE 'Gamepage' SET 'viewcount' = 4 WHERE 'id' = 1

    MySQL said: Documentation
    #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Gamepage' SET 'viewcount' = 4 WHERE 'id' = 1' at line 1

    Whats up? =/
     
    Skillman13, Oct 27, 2009 IP
  11. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #11
    please share table structure using that query..

    let me see the structure then only i can tell you exact query.

    don't you even try to change the query based on the table?
     
    mastermunj, Oct 27, 2009 IP
  12. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    omg, so sorry. Got it working now, :( Silly column names, i swapped them around...

    I should listen to you more and doubt myself...


    Thanks,
     
    Skillman13, Oct 27, 2009 IP
  13. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #13
    that's okie... keep it cool while working. such problems always take away our energy in agitation..
     
    mastermunj, Oct 27, 2009 IP
  14. youngone324

    youngone324 Peon

    Messages:
    125
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    $id = $_GET['id'];

    $update_query = "UPDATE Gamepage SET viewcount = viewcount + 1 where id = '$id'";
    $result=mysql_query($update_query);



    try that
     
    youngone324, Oct 27, 2009 IP