[Problem] Inventory/store! -Solved

Location of the Videos
Post Reply
dlsmoker
Posts: 7
Joined: Wed Jan 04, 2012 12:03 pm

[Problem] Inventory/store! -Solved

Post by dlsmoker »

Sorry if I insist with topics, but on the video topic I got no answer.

This is my code
battle.php

Code: Select all

<?
require_once('config.php');

if (isset($_SESSION['username']))
{
	$player = $_SESSION['username'];
} else
	{
		echo "You are not logged in. <a href='../login.html'>Click</a> to log in";
		exit;
	}

$info = "SELECT * FROM `game`.`users` where username = '$player'";
$info2 = mysql_query($info) or die ("Could not get player stats"); 
$info3 = mysql_fetch_array($info2);

$playhp = $info3['hpoints'];
$playattack = $info3['attack'];
$playdefense = $info3['defense'];

/*--------------------------RANDOM ID----------------------*/

if (isset($_GET['randid']))
	{
		$randid = $_GET['randid'];
		$iteminfo = "SELECT * from inventory where randid='$randid'";
		$iteminfo2 = mysql_query($iteminfo) or die ("Could not get item random id");
		$iteminfo3 = mysql_fetch_array($iteminfo2);
	}
	if ($iteminfo3['name'])
		{
			
		} else
			{
				$name = $iteminfo3['name'];
				$stats = $iteminfo3['stats'];
				$statadd = $iteminfo3['statadd'];
				$type = $iteminfo3['type'];
				if ($type == "healing")
					{
						$newhp = $statadd + $playhp;
						
						if ($newhp > $info3['maxhp'])
							{
								$newhp = $info3['maxhp'];
							}
						$updateplayer = "UPDATE users set hpoints = '$newhp' where username = '$player'";
						mysql_query($updateplayer) or die ("Could not update player HPoints");
						
						$updateitem = "DELETE from inventory where name = '$name' AND randid = '$randid' limit 1";
						mysql_query($updateitem) or die ("Could not delete item");
						
						$playhp = $newhp;
						echo $name. "used and recovered ".$statadd." HP.<br>";
					}
			}

if (isset($_GET['creature']))
	{
		$creature = $_GET['creature'];
		$cinfo = "SELECT *from creatures where name = '$creature'";
		$cinfo2 = mysql_query($cinfo) or die ("Could not get creature stats");
		$cinfo3 = mysql_fetch_array($cinfo2);
	} else
		{
			$cinfo = "SELECT *from creatures order by rand() limit 1";
			$cinfo2 = mysql_query($cinfo) or die ("Could not get creature stats");
			$cinfo3 = mysql_fetch_array($cinfo2);
		}
		
$creature = $cinfo3['name'];
$chp = $cinfo3['hpoints'];
$cattack = $cinfo3['attack'];
$cdefense = $cinfo3['defense'];

/*---------------------------PLAYER INFO----------------------*/
echo "<b>".$info3['username'] ."</b>, lvl ".$info3['level']."<br>";
echo "HP: " . $playhp ."<br>";
echo "Attack: " . $playattack ."<br>";
echo "Defense: " . $playdefense ."<br><br><hr>";
/*---------------------------CREATURE INFO---------------------*/
echo "<b>". $cinfo3['name'] ."</b>, lvl ".$cinfo3['level']."<br>";
echo "HP: " . $chp ."<br>";
echo "Attack: " . $cattack ."<br>";
echo "Defense: " . $cdefense ."<br><br>";

echo "<a href='attack.php?creature=$creature'>Attack!</a>";
echo "<br><a href='useitem.php?creature=$creature'>Use Item!</a>";
echo "<br><a href='store.php?creature=$creature'>Go To Store!</a>";

?>
useitem.php //my inventory

Code: Select all

<?
require_once("config.php");

if (isset($_SESSION['username']))
{
	$player = $_SESSION['username'];
	
} else
	{
		echo "You are not logged in. <a href='../login.html'>Click</a> to log in";
		exit;
	}

	$creature = $_GET['creature'];
	
$info = "SELECT * FROM users where username='$player'";
$info2 = mysql_query($info) or die ("Could not get player stats"); 
$info3 = mysql_fetch_array($info2);

$playerid = $info3['id'];
//echo $player." id is".$playerid."!<br>";

$counter = 0;

echo "<small>";
	echo "<center>";
	echo "<table border='0' width='70' cellspacing='20'>";
	echo "<tr><td width='25%' valign='top'></td>";
	echo "<td valign='top' width='75%'>";
	$invinfo = "SELECT * from inventory where id = '$playerid'";
	$invinfo2 = mysql_query($invinfo) or die ("Could not select player invntory");
	echo "<table border='1' bordercolor='#ccc' bgcolor='#ffffff'";
	echo "<tr><td>Name<font color='ffffff'>__________________</font></td><td>Stat<font color='ffffff'>_______</td><td>Stat Add<font color='ffffff'>_______</td><td>Type<font color='ffffff'>______________</td><td><font color='ffffff'></td><td>";
	while ($invinfo3=mysql_fetch_array($invinfo2))
	{
		echo "<tr><td>$invinfo3[name]</td><td>$invinfo3[stats]</td><td>$invinfo3[statadd]</td><td>$invinfo3[type]</td><td><a href='battle.php?randid=$invinfo[randid]&creature=$creature'>Use Item</td></tr>";
		$counter = 1;
	}
	
	echo "</table></td></tr></table></small>";
	
	if ($counter == 0)
	{
		echo "<center>You're inventory is empty!<br>";
		echo "<a href=battle.php?creature='$creature'>Go Back..</center>";
		exit;
	}
	
	echo "<a href=battle.php?creature='$creature'>Go Back..</center>";
?>
store.php

Code: Select all

<?
require_once("config.php");

if (isset($_SESSION['username']))
{
	$player = $_SESSION['username'];
	
} else
	{
		echo "You are not logged in. <a href='../login.html'>Click</a> to log in";
		exit;
	}

	$creature = $_GET['creature'];
	$counter = 0;
	
	$info = "SELECT * from users where username = '$player'";
	$info2 = mysql_query($info) or die ("Could not select player");
	$info3 = mysql_fetch_array($info2);

	echo "<small>";
	echo "<center>";
	echo "<table border='0' width='70' cellspacing='20'>";
	echo "<tr><td width='25%' valign='top'></td>";
	echo "<td valign='top' width='75%'>";
	$storeinfo = "SELECT * from store where amount > 0";
	$storeinfo2 = mysql_query($storeinfo) or die ("Could not select anything from store");
	echo "<table border='1' bordercolor='#ccc' bgcolor='#ffffff'";
	echo "<tr><td>Name<font color='ffffff'>__________________</font></td><td>Stat<font color='ffffff'>_______</td><td>Stat Add<font color='ffffff'>_______</td><td>Price<font color='ffffff'>______________</td><td><font color='ffffff'></td><td>";
	while ($storeinfo3 = mysql_fetch_array($storeinfo2))
	{
		echo "<tr><td>$storeinfo3[name]</td><td>$storeinfo3[stats]</td><td>$storeinfo3[statadd]</td><td>$storeinfo3[price]</td><td><a href='buyitem.php?randid=$storeinfo3[randid]&creature=$creature'>Buy Item</td></tr>";
		$counter = 1;
	}
	echo "</table></td></tr></table></small>";
	
	if ($counter == 0)
	{
		echo "<center>Store is empty!<br>";
		echo "<a href=battle.php?creature='$creature'>Go Back..</center>";
		exit;
	}
	
	echo "<a href=battle.php?creature='$creature'>Go Back..</center>";
?>
and buyitem.php

Code: Select all

<?
require_once('config.php');

if (isset($_SESSION['username']))
{
	$player  =  $_SESSION['username'];
} else
	{
		echo "You are not logged in. <a href = '../login.html'>Click</a> to log in";
		exit;
	}

  $creature  =  $_GET['creature'];
  $randid  =  $_GET['randid'];

$info  =  "SELECT * from users where username = '$player'";
$info2 = mysql_query($info) or die("Could not get player stats!");
$info3 = mysql_fetch_array($info2);

$playerid  =  $info3['id'];

$iteminfo = "SELECT * from store where randid = '$randid'";
$iteminfo2 = mysql_query($iteminfo) or die("Could not get item stats!");
$iteminfo3 = mysql_fetch_array($iteminfo2);

$name  =  $iteminfo3['name'];
$stats  =  $iteminfo3['stats'];
$statadd  =  $iteminfo3['statadd'];
$type  =  $iteminfo3['type'];
$randid2  =  rand(1000,999999999);

$itembought  =  "INSERT into inventory(id, name, stats, statadd, randid,type) VALUES ('$playerid','$name','$stats','$statadd','$randid2','$type')";
mysql_query($itembought) or die("Could not insert item into inventory");

echo $name . " Purchased";
 echo "<center><a href = 'battle.php?creature = $creature'>Go Back</center>";

  
  ?>
There are severals problems.
1. When I want to fight, I can disply player and monster status, I can battle, but there are some errors:
Notice: Undefined variable: iteminfo3 in C:\xampp\htdocs\game\php\battle.php on line 30
Notice: Undefined variable: iteminfo3 in C:\xampp\htdocs\game\php\battle.php on line 35
Notice: Undefined variable: iteminfo3 in C:\xampp\htdocs\game\php\battle.php on line 36
Notice: Undefined variable: iteminfo3 in C:\xampp\htdocs\game\php\battle.php on line 37
Notice: Undefined variable: iteminfo3 in C:\xampp\htdocs\game\php\battle.php on line 38

2. I can buy ONE item from the store, but than if I buy the 2nd, it won't add/display in the inventory table, nor in the table data base. The error is on mysql_query() or die(Could not insert item into inventory).
I get the message "Could not insert item into inventory" when I want to buy another item. If I delete the first one, then I can buy 1 item, but the 2nd doesn't add, query failed.

3. When I use items (potions), my stats don't update

4. If my inventory is empty and I click the "Go Back button" I get errors:
Notice: Undefined variable: iteminfo3 in C:\xampp\htdocs\game\php\battle.php on line 30
Notice: Undefined variable: iteminfo3 in C:\xampp\htdocs\game\php\battle.php on line 35
Notice: Undefined variable: iteminfo3 in C:\xampp\htdocs\game\php\battle.php on line 36
Notice: Undefined variable: iteminfo3 in C:\xampp\htdocs\game\php\battle.php on line 37
Notice: Undefined variable: iteminfo3 in C:\xampp\htdocs\game\php\battle.php on line 38
Could not get creature stats (mysql_query() failed)

I hope you can help me, and again I am sorry for opening this 2nd topic. Thank you.
Last edited by dlsmoker on Sat Jan 07, 2012 7:27 pm, edited 1 time in total.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: [Problem] Inventory/store!

Post by Jackolantern »

1. It sounds like your iteminfo3 array is not receiving anything from the database. Set up an echo right before you query to echo out what you are sending your database. Echo out the full SQL statement, and then copy it out of the browser. Head to phpMyAdmin, and run the SQL statement there (under the SQL tab) to see what is actually coming back.

2. On the line where are inserting values, you need to remove the single-quotes around any variable that is going to be inserting a numeric value. You cannot put single-quotes around numeric values or you make them into strings, which would cause a problem with your MySQL column types.

I will check out the other 2 in a little bit hopefully. I tried to look at #3, but the code in the useitem.php script was extremely hard to make heads or tails of.
The indelible lord of tl;dr
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: [Problem] Inventory/store!

Post by Chris »

Jackolantern wrote:1. It sounds like your iteminfo3 array is not receiving anything from the database. Set up an echo right before you query to echo out what you are sending your database. Echo out the full SQL statement, and then copy it out of the browser. Head to phpMyAdmin, and run the SQL statement there (under the SQL tab) to see what is actually coming back.
I looked at the code and couldn't make head nor tale of it. Dlsmoker is setting $iteminfo3 between an if statement which retrieves a random item from the database from a GET parameter 'randid', only when it is set. It isn't used further on in the code.

I could easily help, but first of can't make sense of what half of the scripts are supposed to do, besides what the name says they do. I also haven't watched Halls' tutorials. So can't compare them with anything.

If you could explain step by step, what you want the scripts to do, I could help you out better.
Fighting for peace is declaring war on war. If you want peace be peaceful.
dlsmoker
Posts: 7
Joined: Wed Jan 04, 2012 12:03 pm

Re: [Problem] Inventory/store!

Post by dlsmoker »

//nevermind I solved
Post Reply

Return to “Older Browser MMO Videos”