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
https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/
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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.