Help: Updating multiple rows in a table

Discussion in 'PHP' started by Skillman13, Nov 18, 2009.

  1. #1
    I have this code, I want it to increase the 'users' hp by 5 while keeping their hp below 'maxhp';

    Currently it doesn't work, can anyone fix the code below or give me a completely different way to do it?

    mysql_query("UPDATE Zombie SET `Hp` = `Hp` + '5'");

    $result = mysql_query("SELECT * FROM `Zombie`");

    while($row = mysql_fetch_array($result)) {
    if ($row['Hp'] >= $row['Maxhp']) {
    $row['Hp'] = $row['Maxhp'];
    }
    $test = $row['Hp'];
    echo $row['Maxhp'];
    mysql_query("UPDATE Zombie SET `Hp` = '$test'");
    }

    I don't mind how -as long as it works,

    Thanks alot,

    James
     
    Skillman13, Nov 18, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    
    UPDATE Zombie SET Hp = if(Hp + 5 > maxhp, maxhp, Hp + 5)
    
    Code (markup):
    Above query considers maxhp as another column in table, if it is not the case, replace it with php variable and its done :)
     
    mastermunj, Nov 19, 2009 IP
  3. cignusweb

    cignusweb Peon

    Messages:
    147
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    use link this
    "UPDATE Zombie SET Hp = if(Hp + 5 > maxhp, maxhp, Hp + 5)"
     
    cignusweb, Nov 19, 2009 IP
    Skillman13 likes this.
  4. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks :) Working now.
     
    Skillman13, Nov 19, 2009 IP