Jump to content

managing "hit points" in a simple game


matthewst

Recommended Posts

why doesn't this work?

i click the button's but it doesn't do anything

<?php
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("test_game") or die(mysql_error());
?>
<?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'];
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="" method="post" name="FormName" target="_self"><input name="hit_player_2" type="button" 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="button" 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"; 
} 
if(isset($_POST['hit_player_2'])){ 
     $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = player_2"; ; 
} 
//if($submit)
//{
//$sql = "INSERT INTO whiteboard_dates (rest_id, ad_copy_rec) VALUES ('$table_id', '$ad_copy_rec_new')";
//$result = mysql_query($sql);
//}			
?>
</body>
</html>

Link to comment
Share on other sites

$sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = player_2";

 

ithink hit_points-1 is invalid

 

No...that should work fine, I think =/

 

 

think another because thats wrong try that

Link to comment
Share on other sites

It works!!!....kind of

 

it is subtracting hit points now but

 

if i click "hit player 2" it will take one away from player 1

if i click "hit player 2" again it then takes away from player 2

 

it does the same thing for "hit player 2"

Link to comment
Share on other sites

here is the updated 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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.