Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2006
    Posts
    48
    Thanks
    3
    Thanked 1 Time in 1 Post

    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.


    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_2'])){ 
         $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_2'"; 
         mysql_query($sql)or die(mysql_error());
    }
    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());
    }
    ?>
    </body>
    </html>

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,380
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    first of all, you did set a value in your form:
    value="hit_player_2"

    doing that, you are always setting the value, so
    if(isset($_POST['hit_player_2'])) is always true.

    same for player 1.

    ===================

    I think you should ditch the <form> and use links instead.
    <a href="yourscript.php?p=1">hit player one</a>
    <a href="yourscript.php?p=2">hit player two</a>

    (Maybe create some nice graphic buttons for those "hit" links)

    Read in your GET variables from the URL ....

    if($_GET['p'] == 2){
    $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_2'";
    mysql_query($sql)or die(mysql_error());
    }
    if($_GET['p'] == 1){
    $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_1'";
    mysql_query($sql)or die(mysql_error());
    }

    ==================

    Finally,
    Using sessions, you could then allow only one hit per page visit.
    Once "hit", the buttons would disappear. No refresh would help.
    They would have to exit and return to "re-hit" the buttons.
    Last edited by mlseim; 07-05-2007 at 10:44 PM.

  • #3
    New Coder
    Join Date
    Nov 2006
    Posts
    48
    Thanks
    3
    Thanked 1 Time in 1 Post
    You ROCK!!!!

    game.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>
    <a href="atk.php?p=2">hit player two</a>&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&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
    <a href="atk.php?p=1">hit player one</a>
    </body>
    </html>
    atk.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
    if($_GET['p'] == 2){ 
    $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_2'"; 
    mysql_query($sql)or die(mysql_error());
    }
    if($_GET['p'] == 1){ 
    $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_1'"; 
    mysql_query($sql)or die(mysql_error());
    }
    
    echo "<script type='text/javascript'>
    <!--
    window.location = 'test_game.php'
    //-->
    </script>";
    ?>


  •  

    Posting Permissions

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