Page 1 of 1

Urgent PHP Help, please

Posted: Sat Jul 23, 2005 6:00 pm
by rross46
Hi lads n ladiez. I got some scripts i need urgent help with. I sorta jumped into the deep end as im a php noob, and need help bad, i will really appreciate any help. It was for a college project which i got a summer extention for. Please dont think i am tryng to get ppl to do my homework lol, any help given will be put into the acknowledgements and documented.

The scripts i got are for a text based warfare game, like koc a bit, except far simpler.

the first script i got probs with is-

Code: Select all

<?
(!defined('')) ? die : '';
session_start();
(!isset($_SESSION['user_id'])) ? redirect('index.php?mode=login') : '';
$set = (isset($HTTP_GET_VARS['set'])) ? $HTTP_GET_VARS['set'] : '';
$target_userid = (isset($HTTP_GET_VARS['uid'])) ? intval($HTTP_GET_VARS['uid']) : redirect('index.php?mode=members');
$user_id=$_SESSION['user_id'];
$uresult = mysql_query("SELECT * FROM users
                       WHERE user_id=$user_id");
$user=mysql_fetch_assoc($uresult);
$target = mysql_query("SELECT * FROM users
                                                WHERE user_id = '".$target_userid."'");
                                                $target = mysql_fetch_assoc($target);
if ($target_userid == $_SESSION['user_id'])
        {
                echo 'You cannot attack yourself';
                timed_redirect('index.php?mode=attack&set=no', 2);
                die;
        }
                echo 'Maximum ammount: <b>15</b>
<form action="index.php?mode=attack&uid='.$target_userid.'" method="POST">
                Turns: <input type="int" name="att_turns" size="10" /><br />
<input type="submit" value="submit" name="submit"/>
</form'>
 $attack_turns = (isset($HTTP_POST_VARS['att_turns'])) ? intval($HTTP_POST_VARS['att_turns']):" ;

                if ($user['user_turns'] >= $attack_turns)
                        {
                                if ($attack_turns > 0 && $attack_turns <= 15)
                                        {
                                                        if ($user['user_attack_rating'] > $target['user_defence_rating'])
                                                        {
                                                                $spoils = round(((4.3 * ($target['user_gold']/100)) * $user_turns));
                                                  	echo'!!! '.$user['user_turns'].'<br>';
				echo'!!'.$attack_turns.'<br>';
				echo '!!'.$user_turn.'<br>';
                                                                $user_result = mysql_query("UPDATE users
                                                                        SET user_gold = '".$user_gold+$spoils."',user_turns='".$user_turns-$attack_turns."'
				        WHERE user_id = '".$_SESSION['user_id']."'");
                                                                $target_result = mysql_query("UPDATE users
                                                                        SET user_gold = '".$target_gold-$spoils."'
                                                                        WHERE user_id = '".$target_userid."'");
		//the aboe user_result and target_result, they not working.  not sure if i have set up variables correctly.
                                                                $victory = true;
                                                                $outcome = 'Victory';
                                                                echo "Your troops demolish $target' s army, victory is yours" ;
                                                                echo '<br>You have gained:<br><b>'.$spoils.' Gold</b><br><b></b>';
                                                         } else {
                                                                $victory = false;
                                                                $outcome = 'Defeat';
                                                                echo '!! '.$target.' s army beats back your army in a humiliating defeat!' ;
      				 $user_resultd = mysql_query("UPDATE users  
					SET user_turns='".$user_turn-$attack_turns."'
				      	  WHERE user_id = '".$_SESSION['user_id']."'");
                                                        }
                                                $result = mysql_query("INSERT INTO `attack_log` VALUES ('".$_SESSION['username']."', '".$target['username']."', '".$user['user_attack_rating']."', '".$target['user_def_rating']."', '".$outcome."', '".$spoils."')");
                                                 if ($result)
                                                        {
                                                                echo 'Attack Log Updated!';
                                                                if($user_result)
                                                                        {
                                                                                echo'user Stats updated';
                                                                                 timed_redirect('index.php?mode=main', 10);
                                                                         } else {
                                                      		echo'user stat update failed';
                                                                                 timed_redirect('index.php?mode=main', 10);
                                                                         }
				 if($user_resultd)
                                                                        {
                                                                                echo'user Stats updated';
                                                                                 timed_redirect('index.php?mode=main', 10);
                                                                         } else {
                                                      		echo'user stat update failed';
                                                                                 timed_redirect('index.php?mode=main', 10);
                                                                         }
				if($target_result)
				{
					                 echo'target Stats updated';
                                                                                 timed_redirect('index.php?mode=main', 10);
                                                                         } else {
                                                      		echo'target stat update failed';
                                                                                 timed_redirect('index.php?mode=main', 10);
                                                                         }
                                                         }  
}                       
                  }else{
			echo 'You do not have enough turns';
		                       timed_redirect('index.php?mode=attack', 3);
	}
?>
right i think the prob with this is my vars. i need the user to enter attack turns and store it in a variable. am i doing this correctly??

also if you look at the code where i am comparing variables and data and updating data, is that correct?? i get no errors but it dosent work, my logic is off.

Next script is this-

Code: Select all

<?
session_start();
(!defined('')) ? die : '';
(!isset($_SESSION['user_id'])) ? redirect('index.php?mode=login') : '';
$set = (isset($HTTP_GET_VARS['set'])) ? $HTTP_GET_VARS['set'] : '';
$aresult = mysql_query("SELECT * FROM armoury
                       WHERE weapon_type='attack");
$dresult = mysql_query("SELECT * FROM armoury
                       WHERE weapon_type ='defence");
$result=mysql_query("SELECT * FROM armoury 
		 ORDER BY weapon_type ASC");
echo '<table width="95%" cellpadding="0" cellspacing="0" border="0">
       <tr>
             <td align="center"><b>Weapon</b></td>
             <td align="center"><b>Type</b></td>
             <td align="center"><b>Cost</b></td>
             <td align="center"><b>Strength</b></td>
       </tr>';
while ($armoury = mysql_fetch_array($aresult))
{
        echo '<tr>
             <td align="center">'.$armoury['weapon_name'].'</td>
             <td align="center">'.$armoury['weapon_type'].'</td>
             <td align="center">'.$armoury['weapon_cost'].'</td>
             <td align="center">'.$armoury['weapon_strength'].'</td>
<td align="center"><form  method="POST">
                aquantity: <input type="integer" name="Quantity" size="10"name="quantity" />:<br />

</form><br /> 
            </tr>';
<input type="submit" value="submit" name="submit"><br />
}
 $aquantity = (isset($HTTP_POST_VARS['aquantity'])) ? intval($HTTP_POST_VARS['aquantity']) : '';
 $dquantity = (isset($HTTP_POST_VARS['dquantity'])) ? intval($HTTP_POST_VARS['dquantity']) : '';
          $user = mysql_query("SELECT * FROM users
                                      WHERE user_id = '".$_SESSION['user_id']."'");
               $user = mysql_fetch_assoc($user);
If( $aquantity > 0)
{
        $cost=($armoury['weapon_cost']*$armoury['aquantity']);
        if ($user['user_gold'] >= $cost)
        {
                  $apurchase = mysql_query("INSERT INTO `user_armoury`  VALUES ('".$user_id."','".$user_armoury['weapon_name']."','".$user_armoury['weapon_type']."','".$user_armoury['weapon_strength']."','".$aquantity."' '')");
		if ($apurchase)
		{
                        		$user_attack= mysql_query("UPDATE users
                                         	SET user_attack_rating ='".$user_attack_rating+$weapon_strength*$aquantity*$user_army_size."'	
                                          	WHERE user_id = '".$_SESSION['user_id']."'");
			if ($user_attack)
			{
				echo'attack updated';
			}else{
				echo'attack failed';
			}
       			$user_gold_update= mysql_query(" UPDATE users 
							SET user_gold='".$user_gold-$cost."'
							 WHERE user_id='".$_SESSION['user_id']."'");
			if($user_gold_update)
			{
				echo'gold updated';
			}else{
				echo'gold failed';
			}                
 			echo 'Purchase Successful!';
                                	timed_redirect('index.php?mode=armoury', 5);
		}else {
        			echo 'purchase failed';
		}
	}else{
		echo'not enough gold';
	}
}

while ($armoury = mysql_fetch_array($dresult))
{
        echo '<tr>
             <td align="center">'.$armoury['weapon_name'].'</td>
             <td align="center">'.$armoury['weapon_type'].'</td>
             <td align="center">'.$armoury['weapon_cost'].'</td>
             <td align="center">'.$armoury['weapon_strength'].'</td>
<td align="center"><form  method="POST">
                dquantity: <input type="integer" name="Quantity" size="10"name="quantity" />:<br />

</form><br /> 
            </tr>';
<input type="submit" value="submit" name="submit"><br />
}
 $dquantity = (isset($HTTP_POST_VARS['dquantity'])) ? intval($HTTP_POST_VARS['dquantity']) : '';


If( $dquantity > 0)
{
        $cost=($armoury['weapon_cost']*$armoury['dquantity']);
        if ($user['user_gold'] >= $cost)
        {
                  $dpurchase = mysql_query("INSERT INTO `user_armoury`  VALUES ('".$user_id."','".$user_armoury['weapon_name']."','".$user_armoury['weapon_type']."','".$user_armoury['weapon_strength']."','".$aquantity."' )");
		if ($dpurchase)
		{
                       		$user_defence= mysql_query("UPDATE users
                                          	SET user_defence_rating ='".$user_defence_rating+$weapon_strength*$dquantity*$user_army_size."';
                                          	WHERE user_id = '".$_SESSION['user_id']."'");
        			if ($user_defence)
			{
				echo'defence updated';
			}else{
				echo'defence failed';
			}
        			$user_gold_update= mysql_query( "UPDATE users SET user_gold='".$user_gold-$cost."'
							 WHERE user_id='".$_SESSION['user_id']."'");
			if($user_gold_update)
			{
				echo'gold updated';
			}else{
				echo'gold failed';
			}                
			 echo 'Purchase Successful!';
	                                timed_redirect('index.php?mode=armoury', 5);
		}else {
        			echo 'purchase failed';
		}
	}else{
		echo'not enough gold';
	}
}
?>
This is an armoury script, it takes in items from a table and displays them. they are seperated by attack weapon or defence weapon. i need text boxes next to each one, where the quantity of each weapon is entered, and then the quantity*cost is compared to the users gold, if the user has enough, the items get added to the user_armoury table. it just dont work. flat out, nothing happens.

again i dare say my vars are way off, please could someone cast an eye over this and hopefully tell me where i going wrong.

its a lot to ask, sorry guys im just in desperate need of help lol.

Re: Urgent PHP Help, please

Posted: Sat Jul 23, 2005 7:51 pm
by timvw
Next time choose an informative subject.. As it is summer you have the time for a more in-depth study...

As this code is not in a production environment, you might want to start all your scripts with

Code: Select all

ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

Code: Select all

(!defined('')) ? die : '';
(!isset($_SESSION['user_id'])) ? redirect('index.php?mode=login') : '';
http://www.php.net/manual/en/language.c ... uctures.if

Code: Select all

$set = (isset($HTTP_GET_VARS['set'])) ? $HTTP_GET_VARS['set'] : '';
$target_userid = (isset($HTTP_GET_VARS['uid'])) ? intval($HTTP_GET_VARS['uid']) : redirect('index.php?mode=members');
Nowadays, you should use $_GET['uid'] etc...

http://www.php.net/manual/en/language.v ... efined.php

also if you look at the code where i am comparing variables and data and updating data, is that correct?? i get no errors but it dosent work, my logic is off.
http://www.php.net/manual/en/language.c ... ctures.php