SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    managing hit points in a simple game

    The problem I have is when I click "hit_player_2" it first takes away a point from player one. If I continue to click "hit_player_2" it will then take away points from player 2. "Hit_player_1" does th same thing. I also noticed that it takes away a point from the last player to lose one when I manually refresh.

    PHP Code:
    <?php
    mysql_connect
    ("localhost""root""root") or die(mysql_error());
    mysql_select_db("test_game") or die(mysql_error());
    //error_reporting(E_ALL);
    error_reporting(0);
    ?>
    <?php
    $query 
    "SELECT player_name, hit_points FROM users"
    $result=mysql_query($query);
    while (
    $row mysql_fetch_assoc($result))
    {
    $player_name $row['player_name'];
    $hit_points $row['hit_points'];
    if(
    $hit_points <= "0")
    {
    echo 
    "$player_name is dead&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
    }
    else
    {
    echo 
    "Player: $player_name&nbsp";
    echo 
    "Hit points: $hit_points&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
    }
    }
    ?>
    <title>test game</title>
    </head>
    <body>
    <br><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="FormName"><input name="hit_player_2" type="submit" value="hit_player_2">
    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input name="hit_player_1" type="submit" value="hit_player_1"></form>
    <?php
    if(isset($_POST['hit_player_1'])){ 
         
    $sql "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_1'"
         
    mysql_query($sql)or die(mysql_error());
    }
    if(isset(
    $_POST['hit_player_2'])){ 
         
    $sql "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_2'"
         
    mysql_query($sql)or die(mysql_error());
    }
    ?>
    </body>
    </html>

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Make them two separate forms, or use JavaScript to manage the submitted form info. Both submit buttons are part of the form that's submitted, so both conditions pass and both players lose points.

  3. #3
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Dan!
    that makes sense (both buttons on the same form)

    how would I use javascript in this situation?

  4. #4
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I put the buttons in two seperate forms but it still takes a point away from the wrong player first.

    Code:
    <?php
    mysql_connect("localhost", "root", "root") or die(mysql_error());
    mysql_select_db("test_game") or die(mysql_error());
    //error_reporting(E_ALL);
    error_reporting(0);
    ?>
    <?php
    $query = "SELECT player_name, hit_points FROM users"; 
    $result=mysql_query($query);
    while ($row = mysql_fetch_assoc($result))
    {
    $player_name = $row['player_name'];
    $hit_points = $row['hit_points'];
    if($hit_points <= "0")
    {
    echo "$player_name is dead&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
    }
    else
    {
    echo "Player: $player_name&nbsp";
    echo "Hit points: $hit_points&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
    }
    }
    ?>
    <title>test game</title>
    </head>
    <body>
    <br>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="FormName1"><input name="hit_player_2" type="submit" value="hit_player_2">
    <?php
    if(isset($_POST['hit_player_2'])){ 
         $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_2'"; 
         mysql_query($sql)or die(mysql_error());
    }
    ?>
    </form>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="FormName2"><input name="hit_player_1" type="submit" value="hit_player_1">
    <?php
    if(isset($_POST['hit_player_1'])){ 
         $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_1'"; 
         mysql_query($sql)or die(mysql_error());
    }
    ?>
    </form>
    </body>
    </html>

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Note that you display the number of hit points before you update them, so whatever you see doesn't include the result of the last button click. It'd probably be easier to debug if you changed that order around. There's no reason for the logic to be down with the form in the file


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •