Page 1 of 1

Someone tell me why im getting these undefined empty indexes

Posted: Wed Dec 27, 2006 4:13 am
by Mythic Fr0st
I'll explain the "buying weapon" part

It detects if the form submit button "Buy" is the button being clicked
it sets a variable idw to the value of the weapon dropdown box (not working)
it checks if the gold you have is enough to purchase the weapon "$weaponprices[$idw]"
if you have enough gold it,
then (inv is the inventory) counts to the end of my array "inventory" "$x = count($inv)-1"
subtracts the gold from your current gold and adds the weapon to your inventory
"$inv[$x]=$stweaponlist[$idw];"

else

it displays "You do not have enough gold to purchase blah item"

The errors:
Notice: Undefined index: weapon in c:\program files\easyphp1-8\www\mythic aeons\bankdep.php on line 167

Notice: Undefined index: in c:\program files\easyphp1-8\www\mythic aeons\bankdep.php on line 169

Notice: Undefined index: in c:\program files\easyphp1-8\www\mythic aeons\bankdep.php on line 172

Notice: Undefined variable: stweaponlist in c:\program files\easyphp1-8\www\mythic aeons\bankdep.php on line 173

heres the code

(Note you'll see the //problem starts here below part, although some of the arrays used are up the top (inv is in my database)

Code: Select all

<html>
	<head>
	   <style type='text/css'>
	    body
	    {
	    background-image:
	    url('SilverBG.jpg')
	    }
	    </style>
	</head>
    <body>
    <?php
	$id=$_SESSION['id'];
	$con = mysql_connect("localhost","root","");
	if (!$con)
	  {
	  die('Could not connect: ' . mysql_error());
	  }
	
	mysql_select_db("chars", $con);
	$result = mysql_query("SELECT * FROM chars 
	 WHERE idcount = $id");
	
	
	$row = mysql_fetch_array($result);
        $horz=$row['horz'];
        $vert=$row['vert'];
        $currtown=$row['currtown'];
        $gold=$row['gold'];
        $bank=$row['bankedgold'];
        $currtownwe=$currtown;
        $dse=$row['dse'];
        $egerr=false;
        $egerr2=false;
        $weapon=$row['weapon'];

if (isset($_POST['De1']))
    {
    if ($_POST['De1'] == 'De2')
        {
        $eg=$_POST['Bankopt2'];
         if ($eg < 0)
             {
             if ($gold + $eg >= 0)
                 {
                 $bank=$bank-$eg;
                 $gold=$gold+$eg; 
                 $eg2=substr($eg, 1,strlen($eg));
                 echo '<div style="position: absolute; top: 360; left: 400;"><table border=\'3\' width="325"> <tr> <td align="center"><b>You have deposited '.$eg2. ' gold into your bank</b></td></tr></table></div>';
                 }
                 else
                 {
                 echo '<div style="position: absolute; top: 360; left: 400;"><table border="3" width="325"> <tr> <td align="center"><b>You do not have that much current gold</b></td></tr></table></div>';
                 }
            }
             
         if ($eg > 0)
             {
             if ($eg <= $bank)
                 {
                 echo '<div style="position: absolute; top: 360; left: 400;"><table border=\'3\' width="325"> <tr> <td align="center"><b>You have withdrawn '.$eg. ' gold from your bank</b></td></tr></table></div>';
                 $bank=$bank-$eg;
                 $gold=$gold+$eg;
                 }
                 else
                 {
                 echo '<div style="position: absolute; top: 360; left: 400;"><table border="3" width="325"> <tr> <td align="center"><b>You do not have that much gold in your bank</b></td></tr></table></div>';
                 }
             }  
        }
    }

if ($horz == 4 && $vert == 0)
    {
if ($currtown == "Harkan")
    {
    $dse=false;
    ?>
    <div style='position: absolute; top:295; left:427;'>
    <?php
    echo '<i>'.$currtownwe.' </i>Bank <br /> Use a normal value to withdraw, negative to deposit';
    ?>
    </div>
    <!--<div style='position: absolute; top: 300; left: 425;'>
    <form method="post" action="Harkan.php">
    <input type="text" name='Bankopt1'>
    <input type='hidden' name='With1' value='With2'>
    <input type="submit" value="Withdraw">
    </form>
    </div>-->
    <div style='position: absolute; top: 335; left: 425;'>
    <form method="post" action="Harkan.php">
    <input type="text" name='Bankopt2'>
    <input type='hidden' name='De1' value='De2'>
    <input type="submit" value="Withdraw/Deposit">
    </form>
    </div>
    <?php
    }
    }




        if ($dse==true)
        {
        mysql_query("UPDATE chars SET dse = 1 WHERE idcount = $id")or die ("bankdep1 ".mysql_error());
        }
        else
        {
        mysql_query("UPDATE chars SET dse = 0 WHERE idcount = $id")or die ("bankdep4 ".mysql_error());
        }
        mysql_query("UPDATE chars SET gold = $gold WHERE idcount=$id")or die ("bankdep2 ".mysql_error());
        mysql_query("UPDATE chars SET bankedgold = $bank WHERE idcount=$id")or die ("bankdep3 ".mysql_error());

//////////////////////////////////////////PROBLEM BELOW

	if ($horz == 2 && $vert == 2)
            {
        if ($currtownwe == "Harkan")
	    {
	    $dse=true;
    	    ?>
	    <div style='position: absolute; top: 200; left: 400;'>
            <table border="3">
            <tr>
            <td align="center">
            <b>Weapon shop</b>
            </td>
            </tr>
            <td align="left">
            <form method="post" action="Harkan.php"> 
            <select name="weapon">;
            <option value=0>Iron Dagger, Price: 36g</option>
            <option value=1>Iron Sword, Price: 89g</option>
            <option value=2>Steel Sword, Price: 201g</option>
            <option value=3>Titan Sword, Price: 745g</option>
            </select></form>
            </td>
            </tr>
            </table>
            </div>
            <div style="position: absolute; top: 280; left: 445;">
            <form method="post" action="Harkan.php">
            <input type='hidden' name='req1' value='req2'>
            <input type="submit" value="Requirements">
            </form>
            </div>
            <div style="position: absolute; top: 280; left: 400;">
            <form method="post" action="Harkan.php">
            <input type='hidden' name='Buy1' value='Buy2'>
            <input type="submit" value="Buy">
            </form>
            </div>
            <?php
            }
            }
            else
            {
            $dse=true;
            }
            
            if (isset($_POST['Buy1']))
            {
            if ($_POST['Buy1'] == 'Buy2')
	            {
	        	$idw=$_POST['weapon']; //line 167
	       	
	        	if ($gold >= $weaponprices[$idw]) //line 169
	            	{
		    		$x = count($inv)-1;
		    		$gold=$gold-$weaponprices[$idw]; //line 172
		    		$inv[$x]=$stweaponlist[$idw]; // line 173
		    		}
		    		else
		    		{
		    		echo 'You do not have enough gold to buy '.$stweaponlist[$idw];
		    		}
                }
    	    }

//PROB ABOVE

?>
</body>
</html>

so can anyone tell me my mistakes I dont exactly get why its not working:P

Thanks in advance

- sorry just realized had no php tags:P

Re: Someone tell me why im getting these undefined empty ind

Posted: Wed Dec 27, 2006 4:30 am
by timvw
Mythic Fr0st wrote:I'
Notice: Undefined index: weapon in c:\program files\easyphp1-8\www\mythic aeons\bankdep.php on line 167
Notice: Undefined index: in c:\program files\easyphp1-8\www\mythic aeons\bankdep.php on line 169
Notice: Undefined index: in c:\program files\easyphp1-8\www\mythic aeons\bankdep.php on line 172
Notice: Undefined variable: stweaponlist in c:\program files\easyphp1-8\www\mythic aeons\bankdep.php on line 173

Code: Select all

$idw=$_POST['weapon']; //line 167
	       	
if ($gold >= $weaponprices[$idw]) //line 169
{
 $x = count($inv)-1;
 $gold=$gold-$weaponprices[$idw]; //line 172
 $inv[$x]=$stweaponlist[$idw]; // line 173
}
You haven't checkef if there is really such a variable available in $_POST...

So you will have to start with:

Code: Select all

if (!array_key_exists('weapon', $_POST)) {
  // the value is missing? problem with html form?
} else {
  if (!is_numeric($POST['weapon'])) {
   // the value isn't a number... someone trying to send invalid values?
   } else {
        // now do the work 
   }
}

but

Posted: Wed Dec 27, 2006 4:43 am
by Mythic Fr0st
But they cant enter values, the dropdown box is set specificly set so far to 0,1,2,3 then it refers to the correct weapon

Code: Select all

$_POST weapon gets its value from this
(Coloured in purple is the form part, specific, <select name="weapon">)
if you select "Iron Dagger" It returns 0, then it will get the price $weaponprice[0] (36, which is price of iron dagger) and so on thats were the value for $idw comes from

if ($horz == 2 && $vert == 2)
            {
        if ($currtownwe == "Harkan")
            {
            $dse=true;
            ?>
            <div style='position: absolute; top: 200; left: 400;'>
            <table border="3">
            <tr>
            <td align="center">
            <b>Weapon shop</b>
            </td>
            </tr>
            <td align="left">
            <form method="post" action="Harkan.php">
            <select name="weapon">;
            <option value=0>Iron Dagger, Price: 36g</option>
            <option value=1>Iron Sword, Price: 89g</option>
            <option value=2>Steel Sword, Price: 201g</option>
            <option value=3>Titan Sword, Price: 745g</option>
            </select></form>
    </td>
            </tr>
            </table>
            </div>
            <div style="position: absolute; top: 280; left: 445;">
            <form method="post" action="Harkan.php">
            <input type='hidden' name='req1' value='req2'>
            <input type="submit" value="Requirements">
            </form>
            </div>
            <div style="position: absolute; top: 280; left: 400;">
            <form method="post" action="Harkan.php">
            <input type='hidden' name='Buy1' value='Buy2'>
            <input type="submit" value="Buy">
            </form>
            </div>
            <?php
            }
            }
            else
            {
            $dse=true;
            }
is that right, or still wrong?

Posted: Wed Dec 27, 2006 5:28 am
by jyhm
I would try timvw's suggestion,

For 'good codeness sake' keep them wrapped in an if isset type of statement. I would also test with an echo or a print_r($array) for your array/variables that you are having problems with in order to see what is happening when. Like so,..

Code: Select all

if (!array_key_exists('weapon', $_POST)) {
  // the value is missing? problem with html form?
         echo $weapon; // See what the value is here
         // or if weapon is an array use print_r($weapon); wrapped in <pre> tags
} elseif (!is_numeric($POST['weapon'])) {
      echo $weapon; // See what the value is here too
   // the value isn't a number... someone trying to send invalid values?
   } else {
      echo $weapon; // even here as well, you can always take them out later
        // now do the work Wink
   }
}

Re: but

Posted: Wed Dec 27, 2006 5:43 am
by timvw
Mythic Fr0st wrote:But they cant enter values, the dropdown box is set specificly set so far to 0,1,2,3 then it refers to the correct weapon
You're assuming that will be the only source of requests... Who says they don't fabric their own form that submits data?
Mythic Fr0st wrote: <form method="post" action="Harkan.php">
<select name="weapon">;
Notice that you didn't specify a method attribute in the form, so it defaults to GET... Which means that you would have to look for the value in $_GET['weapon'] and not $_POST['weapon'].

hmm

Posted: Fri Dec 29, 2006 6:33 pm
by Mythic Fr0st
Refering to the <select> part?
<form method="post" action="Harkan.php">
<select name="weapon">;
form "method", its there

anyway, I did it
and it didnt work I got error 169 again O_O and


Notice: Undefined index: weapon in c:\program files\easyphp1-8\www\mythic aeons\bankdep.php on line 169
arraykey =

hmm, Why would that be (goes off to work:D)

Posted: Sat Dec 30, 2006 9:05 am
by RobertGonzalez
You get undefined index errors any time you attempt to reference a variable that is not in existence. Try either initializing your vars to default values or checking if they are set using isset().