Page 2 of 3

Re: Project Z Help thread.

Posted: Tue Jul 13, 2010 9:56 pm
by Corinthius
Thank you all for your help! :)
I've tried a lot of logic debugging, i'm still unsure of the problem. I'm not sure about anyone else, but I always like to rid myself of these bugs before continuing onto something else :lol:

Now; let's see - I've re-done the database, the field is "townhall" and the table is "housing". The value does update whenever the script is run, as it should do. It is entering as a tiny integer.
The whole script is as follows:

Code: Select all

<?php
include_Once '../connect.php';
session_start();
?>

 <?php

if (isset($_SESSION['player'])) 
  {
    $player=$_SESSION['player'];
    $playerinfo="SELECT * from players where name='$player'";
    $playerinfo2=mysql_query($playerinfo) or die("Could not get user stats");
    $playerinfo3=mysql_fetch_array($playerinfo2);
      print "<input type='hidden' name='id' value='$playerinfo3[id]'>";
	 $updaterefresh="update players set lastattacked=0 where ID='$playerinfo3[id]'";
      mysql_query($updaterefresh) or die("Died");
	 }

	else
	{
	print "Sorry, not logged in  please <A href='../login.php'>Login</a><br>";
  exit;
  }

?>
  <link href="../style1.css" rel="stylesheet" type="text/css" />
 

  <div id="centertable01">
 <?php $pid = $playerinfo3['id'];
  $imageid=$_POST['id'];
         $tag=$_POST['tag'];
		 $tag="tag".$tag;
		 $location = $playerinfo3['location'];
  $shname = $_POST['name'];
  
   $numb="SELECT townhall from housing where mapr='$playerinfo3[mapr]' AND mapc='$playerinfo3[mapc]'";
        $numb2=mysql_query($numb) or die("Could not select information");
        $numb3 = mysql_fetch_array($numb2);
    
   if ($numb3['townhall'] == 1)
   {
   echo "There is already a town hall established here!";
   echo "<center><br><A href='build.php'>Go Back";
   exit;
   }
  
  
if ($shname == NULL)
{
echo " You did not enter a name! Try again<br>";
 echo "<center><br><A href='buildtownhall.php'>Go Back";
 exit;
 }
if ($playerinfo3['credits'] < 200000)
		{
	echo "You cannot afford to build a Town Hall here.<br>";
	echo "<center><br><A href='build.php'>Go Back";
	exit;
	}
	else
	{
	$randid = rand(150,500000);
	$SQL = "INSERT into housing(pname, name, townhall, lvl, randid, mapr,mapc) VALUES ('$playerinfo3[name]','$shname','1','1','$randid','$playerinfo3[mapr]','$playerinfo3[mapc]')"; 
      mysql_query($SQL) or die("could not add shop");
	 
	 $updateplayers="Update players set credits=credits-200000 where id='$pid'";
	    mysql_query($updateplayers) or die("Could not update player stats");
		
		 $housing="SELECT * from housing WHERE pname='$playerinfo3[name]' AND randid = $randid";
    $housing2=mysql_query($housing) or die("Could not get housing");
	 $housing3=mysql_fetch_array( $housing2);
		$pid = $housing3['id'];
		$randid = rand(150,500000);
		$SQL = "INSERT into townhalls(pid, pname, name, lvl, randid, greeting) VALUES ('$pid','$playerinfo3[name]','$shname','1','$randid','Welcome to the $shname town hall!')"; 
      mysql_query($SQL) or die("could not add town hall");
	  
	    $SQL = "INSERT into townhallmembers(pid, name, rank,level,glevel) VALUES ('$pid','$playerinfo3[name]','Founder','$playerinfo3[level]',5)"; 
      mysql_query($SQL) or die("could not add council member");
	
	///////////////////////////add to map////////////////////////////////////////  
	 $updateplayers="Update map set house=house+1, mname='$shname' where mapr='$playerinfo3[mapr]' AND mapc='$playerinfo3[mapc]'";
	    mysql_query($updateplayers) or die("Could not update map info");
		
		 $mapinfo="SELECT mapr,mapc,house from map where mapr='$playerinfo3[mapr]' AND mapc='$playerinfo3[mapc]'";
    $mapinfo2=mysql_query($mapinfo) or die("Could not map");
    $mapinfo3=mysql_fetch_array($mapinfo2);
	
	if ($mapinfo3['house'] >= 25)
	{
	 $updateplayers="Update map set tile='metro' where mapr='$playerinfo3[mapr]' AND mapc='$playerinfo3[mapc]'";
	    mysql_query($updateplayers) or die("Could not update player stats");
   }
	elseif ($mapinfo3['house'] >= 15)
	{
	 $updateplayers="Update map set tile='guild4' where mapr='$playerinfo3[mapr]' AND mapc='$playerinfo3[mapc]'";
	    mysql_query($updateplayers) or die("Could not update player stats");
   }
   elseif ($mapinfo3['house'] >= 10)
	{
	 $updateplayers="Update map set tile='guild3' where mapr='$playerinfo3[mapr]' AND mapc='$playerinfo3[mapc]'";
	    mysql_query($updateplayers) or die("Could not update player stats");
   }
   elseif ($mapinfo3['house'] >= 5)
	{
	 $updateplayers="Update map set tile='guild3' where mapr='$playerinfo3[mapr]' AND mapc='$playerinfo3[mapc]'";
	    mysql_query($updateplayers) or die("Could not update player stats");
   }
   elseif ($mapinfo3['house'] >= 2)
	{
	 $updateplayers="Update map set tile='guild2' where mapr='$playerinfo3[mapr]' AND mapc='$playerinfo3[mapc]'";
	    mysql_query($updateplayers) or die("Could not update player stats");
   }
   elseif ($mapinfo3['house'] == 1)
	{
	 $updateplayers="Update map set tile='guild' where mapr='$playerinfo3[mapr]' AND mapc='$playerinfo3[mapc]'";
	    mysql_query($updateplayers) or die("Could not update player stats");
   }
	

/////////////////////////////////////////////////////////////////////////////////////		
		
	  echo "<center><big>A Town Hall has been Built</big></center></br>";
	  echo "<center><br><A href='../locations/map.php'>Go Back";
	}
  ?>
	</div>

Once the first town hall is built, the database updates, adds in the townhall information with the value "1" as it's supposed to.
If someone else tries to construct one, it goes ahead and builds it despite the validation. Hm, any ideas? :?

Re: Project Z Help thread.

Posted: Wed Jul 14, 2010 3:42 am
by Jackolantern
You need to change the function for dealing with the MySQL query resource. Right now you have it as:

Code: Select all

$numb3 = mysql_fetch_array($numb2);
    
   if ($numb3['townhall'] == 1)
   {
   echo "There is already a town hall established here!";
   echo "<center><br><A href='build.php'>Go Back";
   exit;
   }
You are creating a regular array with the results object, which is accessed like $numb3[1] or $numb3[6]. Change it to this:

Code: Select all

$numb3 = mysql_fetch_assoc($numb2);
    
   if ($numb3['townhall'] == 1)
   {
   echo "There is already a town hall established here!";
   echo "<center><br><A href='build.php'>Go Back";
   exit;
   }
mysql_fetch_assoc($resource) gets an associative array, which will let you access the fields the way you are in the IF statement, like $numb3['townhall'].

I didn't work the rest of the way through the code, because this sticks out as a problem. Maybe it can be used that way, but to keep everything straight, I always use mysql_fetch_assoc(). If it still doesn't work, add this line to your code:

Code: Select all

$numb3 = mysql_fetch_array($numb2);

    [b]echo "The value of townhall is: $numb3['townhall']";[/b]

   if ($numb3['townhall'] == 1)
   {
   echo "There is already a town hall established here!";
   echo "<center><br><A href='build.php'>Go Back";
   exit;
   }
...and see what the value is before you are comparing it.

Re: Project Z Help thread.

Posted: Wed Jul 14, 2010 3:14 pm
by Corinthius
No luck still!
I've managed to do a couple of other features while tip-toeing around this little bug. I know that an array is defined by it's key [1], and that the associative array is defined by it's value; thank's for pointing it out! :D

I changed it so that it was pulling out the assoc_Array value, but still no luck, from a logical point of view I can't see the problem, which as a programmer i'm sure you'll understand is frustrating :P

I've added in a value check in the meanwhile, to find the value of the "townhall" field, and this may be a clue to solving the problem (hopefully) - I get this error (using the code you sent me: echo "The value of townhall is: $numb3['townhall']"; );

Code: Select all

Parse error: parse error in C:\wamp\www\Forsaken\build\buildtownhall2.php on line 39
Line 39 is the: small select piece of code put in there: "echo "The value of townhall is: $numb3['townhall']";"

unfortunately, I consulted my senior programmer at college and he's unable to spot the problem (He's more of an ASP.net type of guy)
-
-
-
once again, thank you Jack for your help! It's very much appreciated!

Corinthius.

Re: Project Z Help thread.

Posted: Wed Jul 14, 2010 3:36 pm
by Chris
echo "The value of townhall is: {$numb3['townhall']}";

Re: Project Z Help thread.

Posted: Thu Jul 15, 2010 1:52 am
by Jackolantern
Oops! I forgot about the weirdness with printing associative arrays in echo statements.

Re: Project Z Help thread.

Posted: Thu Jul 15, 2010 3:04 pm
by Corinthius
Thank you all once again, I feel i'm closer to figuring out the problem!

The display statement once corrected does indeed show that the value of "townhall" is 0.
This is intriguing because it says 1 in the database. The names are exactly the same and the datatypes are all correct. I'll look into it more, but if anyone has any ideas, it would be great! :)

Corinthius.

Re: Project Z Help thread.

Posted: Thu Jul 15, 2010 3:55 pm
by Chris
Try find out what type it is passign as?

Code: Select all

echo gettype($numb3['townhall']);
 

Re: Project Z Help thread.

Posted: Thu Jul 15, 2010 5:13 pm
by Corinthius
The type comes out as a string, however, it's a "tinyint" within the database.
Is there a way to update the database and input it as an integer specifically? - as I understand you can do certain things like -

Code: Select all

(int)$variable = 1;
Although, i'm not sure, I will try it :)

What do you think?

Corinthius.

Re: Project Z Help thread.

Posted: Thu Jul 15, 2010 10:25 pm
by Jackolantern
That is called "type casting". What you could do is something like this:

Code: Select all

$holdString = $numb3['townhall'];
$holdInt = (int)$holdString;

if ($holdInt == 1) {
     //etc...
}
Of course the first two lines could be combined as far as I know, but PHP can often be weird with associative string usage, so I typically only empty them into short variables and then work with that.

Re: Project Z Help thread.

Posted: Thu Jul 15, 2010 11:23 pm
by Corinthius
That's interesting, I knew data types could largely affect the execution and over all result of code, but until putting into practice I never knew the extent of damage it could do! xD

Thank you Jack, and Chris, i'll try out the type-casting! :)
I'll give my feed-back once I've tested it! :)


Corinthius.