Struggling with an undefined variable error

Location of the Videos
Post Reply
s0meone
Posts: 21
Joined: Wed Sep 01, 2010 1:06 am

Struggling with an undefined variable error

Post by s0meone »

I keep getting an error on line 32 saying "undefined variable:id cannot insert into backpack. Everything is in the database, spelled correctly and lowercase but I can't figure out what the problem is. Any help is greatly appreciated. Thanks

Code: Select all

<?php
include_once 'connect.php';
 session_start();
 include_once 'logo.php';
?>
<link href="style.css" rel="stylesheet" type="text/css" />
<?php

if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  echo "Not Logged in <br><br> <A href='index.php'>Login</a>";
  exit;
}


if(isset($_GET['id'])) {$id=$_GET['id'];}

$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
include_once 'statpanel.php';
?>
<div id="table">
<?php

$playerid = $playerinfo3['id'];

$iteminfo="SELECT * from weapons where id='$id'";
$iteminfo2=mysql_query($iteminfo) or die("could not get item stats!");
$iteminfo3=mysql_fetch_array($iteminfo2);

if ($playerinfo3['derim'] < $iteminfo3['price'])
{
 echo "You do not have enough Derim for this purchase!";
  echo "<center><a href='battle.php'>Go Back</center>";
  exit;
}

$playerid = $iteminfo3['pid'];
$name = $iteminfo3['name'];
$descr = $iteminfo3['description'];
$dexterity = $iteminfo3['dexterity'];
$price = $iteminfo3['price'];
$hittext = $iteminfo3['hittext'];
$damage = $iteminfo3['damage'];
$inventor = $iteminfo3['inventor'];
$randid = rand(1000,999999999);

$itembought = "INSERT into playerweapons(pid, name, description, dexterity, price, hittext,damage, inventor, randid) VALUES ('$playerid','$name','$descr','$dexterity','$price','$hittext','$damage','$inventor','$randid')";
mysql_query($itembought) or die("could not insert weapon into backpack");

$updateplayer="update players set derim=derim-'$iteminfo3[price]' where name='$player'";
  mysql_query($updateplayer) or die("Could not update player");

echo $name . " Purchased";
 echo "<center><a href='equipment.php'>Go Back</center>";
  
  
  ?>
 </div>
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Re: Struggling with an undefined variable error

Post by ConceptDestiny »

$playerid = $playerinfo3['id'];

$iteminfo="SELECT * from weapons where id='$id'";
Check your declared variables. :)
s0meone
Posts: 21
Joined: Wed Sep 01, 2010 1:06 am

Re: Struggling with an undefined variable error

Post by s0meone »

ConceptDestiny wrote:
$playerid = $playerinfo3['id'];

$iteminfo="SELECT * from weapons where id='$id'";
Check your declared variables. :)

I thought this would have taken care of it:

Code: Select all

if(isset($_GET['id'])) {$id=$_GET['id'];}
Is there somethin wrong on the weaponshop.php before it?

Code: Select all

<?php
include_once 'connect.php';
 session_start();
   include_once 'logo.php';
   ?>
     <link href="style.css" rel="stylesheet" type="text/css" />
<?php
if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  echo "Not Logged in <br><br> <A href='index.php'>Login</a>";
  exit;

}
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
include_once 'statpanel.php';
?>
</div>
<div id="table">
 <?php

 if (isset($_GET['bought']))
 {
  $id=$_GET['id'];
  
  $playerid = $playerinfo3['id'];
  
  $iteminfo="SELECT * from weapons where id='$id'";
$iteminfo2=mysql_query($iteminfo) or die("could not get item stats!");
$iteminfo3=mysql_fetch_array($iteminfo2);

if ($playerinfo3['derim'] < $iteminfo3['price'])
{
 echo "You do not have enough Derim for this purchase!";
  echo "<center><a href='battle.php'>Go Back</center>";
  exit;
}

$name = $iteminfo3['name'];
$damage = $iteminfo3['damage'];
$dexterity = $iteminfo3['dexterity'];
$hittext = $iteminfo3['hittext'];
$description = $iteminfo3['description'];
$inventor = $iteminfo3['inventor'];
$randid = rand(1000,999999999);

$itembought = "INSERT into playerweapons(pid, name, damage, dexterity, hittext,price,description,inventor,randid) VALUES ('$playerid','$name','$damage','$dexterity','$hittext','$price','$description','$inventor','$randid')";
mysql_query($itembought) or die("could not insert weapon into backpack");

$updateplayer="update players set derim=derim-'$price' where name='$player'";
  mysql_query($updateplayer) or die("Could not update player");
  
  echo $name . " Purchased<br>";
 echo "<a href='battle.php'>Go Back</center>";
 }
 
 $bypass = 0;

  if ($bypass != 1)
 {
$counter = 0;

 echo "<small>";
      print "<center>";
      print "<table border='0' width='70%' cellspacing='20'>";
      print "<tr><td width='25%' valign='top'>";
      print "</td>";
      print "<td valign='top' width='75%'>";
      $invinfo="SELECT * from weapons";
      $invinfo2=mysql_query($invinfo) or die("could not select anything from the weapon shop.");
      print "<table border='1' bordercolor='white' bgcolor='#ffffff'>";
      print "<tr><td>Name<font color='ffffff'>__________</td><td>Dex<font color='ffffff'>____</td><td>Dmg<font color='ffffff'>____</td><td>Description<font color='ffffff'>______________</td><td>Price<font color='ffffff'>______</td><td>Purchase<font color='ffffff'>______</td>";
      while($invinfo3=mysql_fetch_array($invinfo2))

      {
      $counter = 1;
      print "<tr><td>$invinfo3[name]</td><td>$invinfo3[dexterity]</td><td>$invinfo3[damage]</td><td>$invinfo3[description]</td><td>$invinfo3[price]</td><td><A href='buyweapon.php?randid=$invinfo3[randid]'>Buy Item</tr>";

      }

      print "</table>";
      print "</td></tr></table>";
      print "</center>";
      echo "</small>";

      if ($counter == 0)
      {
            echo "<center>There is nothing in the store at this time.<br>";
            echo "<a href='battle.php'>Go Back</center>";
            exit;
      }
  echo "<center><a href='battle.php'>NeverMind</center></a>";
 }
  ?>
  </div>
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Struggling with an undefined variable error

Post by Chris »

No.. he even underlined it for you what you did wrong.

You're trying to call a variable "$id", but it doesn't exist, so PHP throws you a warning. The variable you should be calling is "$playerid".

Code: Select all

$iteminfo="SELECT * FROM `weapons` WHERE `id`='$playerid'"; 
Fighting for peace is declaring war on war. If you want peace be peaceful.
s0meone
Posts: 21
Joined: Wed Sep 01, 2010 1:06 am

Re: Struggling with an undefined variable error

Post by s0meone »

:oops: well i feel sheepish. Thanks
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Re: Struggling with an undefined variable error

Post by ConceptDestiny »

s0meone wrote::oops: well i feel sheepish. Thanks
It's cool. Good luck with your project! :)
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Struggling with an undefined variable error

Post by Callan S. »

s0meone wrote::oops: well i feel sheepish. Thanks
Such is the life of a programmer... :)
Post Reply

Return to “Older Browser MMO Videos”