matthewst Posted July 4, 2007 Share Posted July 4, 2007 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 "; echo "Hit points: $hit_points         "; } ?> <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">                            <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> Quote Link to comment https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/ Share on other sites More sharing options...
pocobueno1388 Posted July 4, 2007 Share Posted July 4, 2007 Change this: <form action="" method="post" name="FormName" target="_self"> To: <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="FormName"> Quote Link to comment https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/#findComment-289288 Share on other sites More sharing options...
teng84 Posted July 4, 2007 Share Posted July 4, 2007 <input name="hit_player_2" type="submit" value="submit"> try that type="submit" Quote Link to comment https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/#findComment-289292 Share on other sites More sharing options...
matthewst Posted July 4, 2007 Author Share Posted July 4, 2007 made the suggested changes the page is at least refreshing now but it still doesn't subtract any hit points Quote Link to comment https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/#findComment-289301 Share on other sites More sharing options...
john010117 Posted July 4, 2007 Share Posted July 4, 2007 Put <?php error_reporting(E_ALL); ?> at the very top of the page. See if any error message comes up. Quote Link to comment https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/#findComment-289305 Share on other sites More sharing options...
matthewst Posted July 4, 2007 Author Share Posted July 4, 2007 no errors Quote Link to comment https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/#findComment-289307 Share on other sites More sharing options...
pocobueno1388 Posted July 4, 2007 Share Posted July 4, 2007 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()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/#findComment-289309 Share on other sites More sharing options...
teng84 Posted July 4, 2007 Share Posted July 4, 2007 $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = player_2"; ithink hit_points-1 is invalid Quote Link to comment https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/#findComment-289310 Share on other sites More sharing options...
pocobueno1388 Posted July 4, 2007 Share Posted July 4, 2007 $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 =/ Quote Link to comment https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/#findComment-289312 Share on other sites More sharing options...
teng84 Posted July 4, 2007 Share Posted July 4, 2007 $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 Quote Link to comment https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/#findComment-289314 Share on other sites More sharing options...
matthewst Posted July 4, 2007 Author Share Posted July 4, 2007 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" Quote Link to comment https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/#findComment-289316 Share on other sites More sharing options...
pocobueno1388 Posted July 4, 2007 Share Posted July 4, 2007 Try putting quotes around the player in the query. $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_1'"; Do that to both queries. Quote Link to comment https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/#findComment-289318 Share on other sites More sharing options...
matthewst Posted July 4, 2007 Author Share Posted July 4, 2007 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                  "; } else { echo "Player: $player_name "; echo "Hit points: $hit_points         "; } } ?> <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">                            <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> Quote Link to comment https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/#findComment-289321 Share on other sites More sharing options...
matthewst Posted July 4, 2007 Author Share Posted July 4, 2007 I just noticed it takes away a hit point from the last player to lose one when I manually refresh. Quote Link to comment https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/#findComment-289349 Share on other sites More sharing options...
teng84 Posted July 4, 2007 Share Posted July 4, 2007 add header file on the end of the script Quote Link to comment https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/#findComment-289350 Share on other sites More sharing options...
matthewst Posted July 4, 2007 Author Share Posted July 4, 2007 I don't follow..??.. can you give me an example? Quote Link to comment https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/#findComment-289354 Share on other sites More sharing options...
matthewst Posted July 5, 2007 Author Share Posted July 5, 2007 What kind of header did you have in mind? Does anyone have any examples of the type of header I need? Quote Link to comment https://forums.phpfreaks.com/topic/58345-managing-hit-points-in-a-simple-game/#findComment-290386 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.