Jump to content

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

You have to actually execute the query:

 

<?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());
} 

?>

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>

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.