Database command?

Discussion in 'PHP' started by marktopper, Sep 11, 2011.

  1. #1
    Greetings. Well I am trying to make a page which will add a number to a special database table.


    Like a user on my site have 10 diamonds, but then when he visit a special link to get 5 extra diamonds.


    The database table is "players", and inside that there is the "diamonds".
    The user is definied as by the table "id".


    I want to run the command when the user access the page.


    The command should do the following:
    It should give the player with the ID="1" addiatinal 5 diamonds.


    Can anyone guide me to do this?


    As far as I got right now is this. (Thanks to Damdomy)
    mysql_query("UPDATE `players` SET `diamonds` = `diamonds` + 5");
    PHP:
    The problem with this is that it gives every player + 5 diamonds, and it should only be the user with the ID set to 1.


    I got MSN, gTalk, Skype and TeamViewer.
     
    Solved! View solution.
    marktopper, Sep 11, 2011 IP
  2. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #2
    Just need to put a WHERE clause.

    
    mysql_query("UPDATE `players` SET `diamonds` = `diamonds` + 5 WHERE uid = 1");
    
    
    PHP:
     
    blueparukia, Sep 11, 2011 IP
  3. marktopper

    marktopper Member

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Thanks a lot for the reply.
    I just tried this, but it doesn't work.

    Do I have to change the other of commands?
    Maybe something like this?
    mysql_query("UPDATE `players` WHERE uid = 1 SET `diamonds` = `diamonds` + 5");
    PHP:
     
    marktopper, Sep 11, 2011 IP
  4. #4
    Mine should be right, though there may be a small error somewhere, try:

    
    mysql_query('UPDATE players
     SET diamonds=diamonds + 5
     WHERE uid="1"') or die(mysql_error());
    
    PHP:
     
    blueparukia, Sep 11, 2011 IP
  5. marktopper

    marktopper Member

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    Thanks a lot :D I got it to work now! You are the best1 :D Thanks man!
     
    marktopper, Sep 11, 2011 IP