Jump to content

GET error or session (either / or)


chocopi

Recommended Posts

Below is more source code for one of my pages. The idea is that a random enemy is chosen from the database and when the user submits the form it is meant to take 20 from the enemies hp. the enemy has 100 hp and when attacked it will  go to 80 then 60 etc. However, instead of going to 80 from 100 it goes to -20. im not sure why it is doing this so here is my code:

 

<?php

// Start Session
session_start();

require_once ('page_header.php');

// Declare Variables
$id = $_SESSION['id'];
$username = $_SESSION['username'];
$password = $_SESSION['password'];
$adv = $_SESSION['adv'];
$title = 'Chocopi :: Fight';
$image_root = 'http://members.lycos.co.uk/chocopi/Mark/images/';

// If post, deal damage to enemy and set sessions
if($_POST)
{
	$damage = 20;
	$enemy_hp = $_POST['enemy_hp'];
	$new_enemy_hp = $enemy_hp - $damage;
	$_SESSION['enemy_hp'] = "$new_enemy_hp";
	$_SESSION['enemy_id'] = "$enemy_id";
	$_SESSION['enemy_name'] = "$enemy_name";
	$_SESSION['enemy_desc'] = "$enemy_desc";
	$_SESSION['enemy_item'] = "$enemy_item";
}

// Check is session of enemy id is set so the sessions can be set to variables
if ( isset($_SESSION['enemy_id'] ))
{
$enemy_id = $_SESSION['enemy_id'];
$enemy_hp = $_SESSION['enemy_hp'];
$enemy_name = $_SESSION['enemy_name'];
$enemy_name = ucfirst($enemy_name);
$enemy_desc = $_SESSION['enemy_desc'];
$enemy_desc = ucfirst($enemy_desc);
$enemy_item = $_SESSION['enemy_item'];
} else
{

// Get random enemy stuff from database
$query = @mysql_query("SELECT `id`,`name`,`desc`,`hp`,`item` FROM `enemies` WHERE `id`>0 ORDER BY rand() LIMIT 1");
$row = @mysql_fetch_assoc($query) or die("Invalid query, please contact an administrator!");
$enemy_id = $row['id'];
$enemy_name = $row['name'];
$enemy_name = ucfirst($enemy_name);
$enemy_desc = $row['desc'];
$enemy_desc = ucfirst($enemy_desc);
$enemy_item = $row['item'];
}

if( !isset($_SESSION['enemy_hp'] ))
{
// Destroy $_SESSION['enemy_hp'];
unset($_SESSION['enemy_hp']);
$enemy_hp = $row['hp'];
} else
{
	$enemy_hp = $_SESSION['enemy_hp'];
}

// Check the session is set
if( isset($_SESSION['id']) && isset($_SESSION['username']) && isset($_SESSION['password']) && isset($_SESSION['adv']) )
{
	// Begin html
	print ("<html>\n");
	print ("<head>\n");
	print ("<title>$title</title>\n");
	print ("</head>\n");
	print ("<body>\n");
	print("<form name=\"adv\" method=\"post\" action=\"$_SERVER[php_SELF]\"> \n");
	print ("<center>\n");
	// Get adv
	$query = @mysql_query("SELECT `adv` FROM `Mark` WHERE `id`='$id'");
	$row = @mysql_fetch_assoc($query) or die("Invalid query, please contact an administrator!");
	$adv = $row['adv'];
	// Check if adv <= 0
	if ($adv <= 0)
	{
		print ("You are out of adventures!\n");
		print ("<br /> \n");
		print ("<br /> \n");
		print ("<a href=\"main.php\">Exit</a>\n");
		die();
	}
	// Check if enemy is alive
	if ($enemy_hp <= 0)
	{
		// Check what $enemy_hp is outputting
		print $enemy_hp;
		unset($_SESSION['enemy_hp']);
		unset($_SESSION['enemy_id']);
		unset($_SESSION['enemy_name']);
		unset($_SESSION['enemy_desc']);
		unset($_SESSION['enemy_item']);
		print ("You have defeated $enemy_name\n");
		print ("<br /> \n");
		print ("<br /> \n");
		print ("<a href=\"adv.php\">Adventure Again</a>\n");
		print ("<br /> \n");
		print ("<br /> \n");
		print ("<a href=\"main.php\">Exit</a>\n");
		die();
	}
	// Minus 1 from adv
	$adv--;
	$adv_update = "UPDATE `Mark` SET `adv`='$adv' WHERE `id`='$id'";
	@mysql_query($adv_update) or die("Invalid query, please contact an administrator!");
	print ("$enemy_name \n");
	print ("<br /> \n");
	print ("<img src=\"$image_root$enemy_id.gif\" alt=\"$enemy_name\" border=\"0\" />\n");
	print ("<br /> \n");
	print ("<br /> \n");
	print ("$enemy_desc \n");
	print ("<br /> \n");
	print ("<br /> \n");
	print ("$enemy_name has $enemy_hp hp.\n");
	print ("<br /> \n");
	print ("$enemy_name will drop item $enemy_item. \n");
	print ("<br /> \n");
	print ("<br /> \n");
	print ("<input type=\"submit\" name=\"attack\" value=\"Attack $enemy_name\" />\n");
	print ("<br /> \n");
	print ("<br /> \n");
	print ("<br /> \n");
	print ("You have $adv adventures left. \n");
	print ("</center>\n");
	print ("</form>\n");
	print ("</body>\n");
	print ("</html>\n");
} else
	{
		// If the session is not set, redirect
		$filename = "index.php";
		header("Location: ".$filename);
	}
?>

 

Thanks,

 

~ Chocopi

Link to comment
Share on other sites

Hi

this part of the code is setting the enemy hp

<?php
// If post, deal damage to enemy and set sessions
if($_POST)
{
	$damage = 20;
	$enemy_hp = $_POST['enemy_hp'];
	$new_enemy_hp = $enemy_hp - $damage;
	$_SESSION['enemy_hp'] = "$new_enemy_hp";
	$_SESSION['enemy_id'] = "$enemy_id";
	$_SESSION['enemy_name'] = "$enemy_name";
	$_SESSION['enemy_desc'] = "$enemy_desc";
	$_SESSION['enemy_item'] = "$enemy_item";
}
?>

 

could you do me a favour and put the following line of code above the $damage = 20;

print_r($_POST);

 

and see if the posted values are what you expect first of all because it seem that you are not getting anything posted across for $_POST['enemy_hp']; because it is taking 20 away from 0 and coming up, quite rightly with, -20.

 

a couple of other points in your code though, wont help this problem but may help in the future, the lines are 43/44/75/76 and 109, all these lines start with the "@" symbol which suppresses errors then you tell it to tell you what the error is, try not to use this, good code will not throw errors anyway and errors are good for finding faults in bad code, just a piece of advice.

Link to comment
Share on other sites

I dont think its showing anything.

 

I have tried everything i know to get $enemy_hp to show up as its value when its posted but it wont work.

 

The reason I have the @ is so users will only see the message "Invalid query, please contact an administrator!" instead of the actual error message.

 

By the way, thanks for helping  ;D

 

~ Chocopi

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.