Update database to increment by one

Discussion in 'MySQL' started by sg552, Sep 10, 2008.

  1. #1
    Is this the correct code for updating databases and increment count column by one?

    $query = "UPDATE 'downloads' SET 'count' = 'count' +1 WHERE 'ip' = '$_SERVER[REMOTE_ADDR]'";
    PHP:
    I set my ip column to primary key, varchar(15), not null and my count column to
    int(11) and not null.

    when I run this code in php and check my phpmyadmin, nothing changes:confused:

    Thanks in advance
    :)
     
    sg552, Sep 10, 2008 IP
  2. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #2
    use this one, never use single quotes around column names

    $query = "UPDATE downloads SET count = count +1 WHERE ip = '{$_SERVER["REMOTE_ADDR"]}'";
    Code (markup):
     
    mwasif, Sep 10, 2008 IP
    sg552 likes this.
  3. sg552

    sg552 Peon

    Messages:
    187
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3

    thanks but when I run the code my count column in phpmyadmin is still 0? You sure count +1 will work...:confused:

    When I run the code, the code will check my IP and increment count to 1, I run the code again increment count to 2 and so on....

    Thanks
    :)
     
    sg552, Sep 10, 2008 IP
  4. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #4
    This is a PHP so this code will work in code but not in phpmyadmin directly.
     
    mwasif, Sep 10, 2008 IP
  5. sg552

    sg552 Peon

    Messages:
    187
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    sg552, Sep 10, 2008 IP
  6. sslshopper

    sslshopper Member

    Messages:
    58
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #6
    mwasif is saying that you have to run the code in PHP first and then check it in phpmyadmin. I imagine that is what you are doing but if you just want to enter a query directly in phpmyadmin you can run this to verify that it works:

    UPDATE downloads SET count = count +1 WHERE ip = '124.82.48.70';
    Code (markup):
     
    sslshopper, Sep 10, 2008 IP
    sg552 likes this.
  7. greny

    greny Peon

    Messages:
    162
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    UPDATE downloads SET count = count +1 WHERE ip = '124.82.48.70';
    Code (markup):
    The SQL query is valid and should work. Check if 'count' is int(11) and NULL is set to YES. Test it in phpMyAdmin.
     
    greny, Sep 10, 2008 IP
    sg552 likes this.
  8. sg552

    sg552 Peon

    Messages:
    187
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    problem solved :D thanks for all your help!!
     
    sg552, Sep 10, 2008 IP
  9. nirajkum

    nirajkum Active Member

    Messages:
    815
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #9
    wat was the problem .... was it "NULL" factor ????
     
    nirajkum, Sep 10, 2008 IP
  10. chisara

    chisara Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Be sure to sanitise you input, the $_SERVER["REMOTE_ADDR"] part is untrusted user input. This means you are leaving yourself open to an SQL injection attack.
    Try a search on google for securityfocus sanitize $_SERVER["REMOTE_ADDR"]
    Sorry for the extra work I need to wait a bit before I can make links or something.
     
    chisara, Sep 15, 2008 IP