Jump to content

Recommended Posts

im not sure whats wrong with this script its supposed to be a fighting script and i had it kinda working before but im not sure whats wrong with it

 

 


session_start();

if(!isset($_GET['NPC']))
{
echo "You have not selected a NPC to fight! Please select one from below <br><br>";

$NPCS = mysql_query("SELECT * FROM `npc`");
while($Show = mysql_fetch_array($NPCS))
	{
	echo "<a href=\"Attack.php? Action=Attack" . $Show['npcname'] . "\">" . $Show['npcname'] . "</a><br>";
	}
exit();
}

$NPC = mysql_escape_string($_GET['NPC']);

echo "<a href=\"Attack?NPC=" . $NPC . "&Action=Attack\">Attack</a><br>
<a href=\"Attack?NPC=" . $NPC . "&Action=Run\">Run</a><br>";

$username = $_COOKIE['ID_my_site'];

if(isset($_COOKIE['NPC']))
{
$UserS = mysql_query("SELECT * FROM `users` WHERE username='$username'");
$UserS = mysql_fetch_array($UserS);
$UserH = $_COOKIE['health'];
$UserA = $UserS['skill'];
$UserA *= .1;

$NPCS = mysql_query("SELECT * FROM `npc` WHERE npcname='$NPC'");
$NPCS = mysql_fetch_array($NPCS);
$NPCH = $_COOKIE['NPCH'];
$NPCA = $NPCS['dmg'];
$NPCA *= .1;
$NPCX = $NPCS['skillgained'];

$NPCH -= $UserA;

if($NPCH <= 0)
	{
	echo "You win and have gained " . $NPCX . " skill";

	$UserA = $UserS['skill'] + $NPCX;

	mysql_query("UPDATE `users`
	SET skill='$UserA'
	WHERE username='$username'");

	unset($_COOKIE['NPC']);

	exit();
	}

setcookie('NPCH', $NPCH, 2400);

$UserH -= $NPCA;

if($UserH <= 0)
	{
	echo "You have lost";

	$UserA = $UserA * .95;

	mysql_query("UPDATE `users`
	SET skill='$UserA'
	WHERE username='$username'");

	unset($_COOKIE['NPC']);

	exit();
	}

setcookie('health', $UserH, 2400);
}
else
{
$UserS = mysql_query("SELECT * FROM `users` WHERE username='$username'");
$UserS = mysql_fetch_array($UserS);

$NPCS = mysql_query("SELECT * FROM `npc` WHERE npcname='$NPC'");
$NPCS = mysql_fetch_array($NPCS);

setcookie('NPC', $NPC, 2400);
setcookie('health', $UserS['health'], 2400);
setcookie('skill', $UserS['skill'], 2400);
setcookie('NPCH', $NPCS['health'], 2400);
}

?>


</body>
</html>



Link to comment
https://forums.phpfreaks.com/topic/135803-need-help-fixing-this-php-script/
Share on other sites

  • Replies 67
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

When asking for help with a script it is always advisable to supply as much information as possible.

 

You know what your script should or shouldn't be doing - we're not psychics or magicians and when looking at others' code need to know a little more than "it isn't working"

the script is supposed to take player data player health "A" and skill "B" and then subtract 10% of "B" from "C" which is computer controlled enemy and then take 10% of "D" which is computer controlled enemy skill  and subtract that from "A" until either "A" or "C" reaches zero and if "A" reaches zero then 5% of "B" is supposed to be taken away from "B" and if "C" reaches zero then "B" is supposed to gain a preset number to the total of "B". im not sure whats wrong with it all i know is that i keeps telling me that i havent picked a npc to fight

I'm not 100% if this is right seeing as you don't know the code either but try this

 

change

 

<?php
if(!isset($_GET['NPC']))
   {
   echo "You have not selected a NPC to fight! Please select one from below <br><br>";

   $NPCS = mysql_query("SELECT * FROM `npc`");
   while($Show = mysql_fetch_array($NPCS))
      {
      echo "<a href="Attack.php? Action=Attack" . $Show['npcname'] . "">" . $Show['npcname'] . "</a><br>";
      }
   exit();
   }
?>

 

to this

 

<?php
if(!isset($_GET['NPC']))
   {
   echo "You have not selected a NPC to fight! Please select one from below <br><br>";

   $NPCS = mysql_query("SELECT * FROM `npc`");
   while($Show = mysql_fetch_array($NPCS))
      {
      echo "<a href="Attack.php? Action=Attack&NPC=" . $Show['npcname'] . "">" . $Show['npcname'] . "</a><br>";
      }
   exit();
   }
?>

You've got this line:

if(!isset($_GET['NPC']))

 

That is checking if NPC is in the URL but you don't seem to actually read it anywhere. You do also check for a cookie with this line:

if(isset($_COOKIE['NPC']))

 

But again, you don't actually read it.

here is the code i had that sorta worked before

it might be easier to fix from this point on i hope

 

<?php

session_start();

if(!isset($_GET['NPC']))
{
echo "You have not selected a NPC to fight! Please select one from below <br><br>";

$NPCS = mysql_query("SELECT * FROM `npc`");
while($Show = mysql_fetch_array($NPCS))
	{
	echo "<a href=\"Attack.php?NPC=" . $Show['npcname'] . "\">" . $Show['npcname'] . "</a><br>";
	}
exit();
}

$NPC = mysql_escape_string($_GET['NPC']);

echo "<a href=\"Attack?NPC=" . $NPC . "&Action=Attack\">Attack</a><br>
<a href=\"Attack?NPC=" . $NPC . "&Action=Run\">Run</a><br>";

$username = $_COOKIE['ID_my_site'];

if(isset($_COOKIE['NPC']))
{
$UserS = mysql_query("SELECT * FROM `users` WHERE username='$username'");
$UserS = mysql_fetch_array($UserS);
$UserH = $_COOKIE['health'];
$UserA = $UserS['skill'];
$UserA *= .1;

$NPCS = mysql_query("SELECT * FROM `npc` WHERE npcname='$NPC'");
$NPCS = mysql_fetch_array($NPCS);
$NPCH = $_COOKIE['NPCH'];
$NPCA = $NPCS['dmg'];
$NPCA *= .1;
$NPCX = $NPCS['skillgained'];

$NPCH -= $UserA;

if($NPCH <= 0)
	{
	echo "You win and have gained " . $NPCX . " skill";

	$UserA = $UserS['skill'] + $NPCX;

	mysql_query("UPDATE `users`
	SET skill='$UserA'
	WHERE username='$username'");

	unset($_COOKIE['NPC']);

	exit();
	}

setcookie('NPCH', $NPCH, 2400);

$UserH -= $NPCA;

if($UserH <= 0)
	{
	echo "You have lost";

	$UserA = $UserA * .95;

	mysql_query("UPDATE `users`
	SET skill='$UserA'
	WHERE username='$username'");

	unset($_COOKIE['NPC']);

	exit();
	}

setcookie('health', $UserH, 2400);
}
else
{
$UserS = mysql_query("SELECT * FROM `users` WHERE username='$username'");
$UserS = mysql_fetch_array($UserS);

$NPCS = mysql_query("SELECT * FROM `npc` WHERE npcname='$NPC'");
$NPCS = mysql_fetch_array($NPCS);

setcookie('NPC', $NPC, 2400);
setcookie('health', $UserS['health'], 2400);
setcookie('skill', $UserS['skill'], 2400);
setcookie('NPCH', $NPCS['health'], 2400);
}

?>

here is the url were this script should running http://narutotalesofthesannin.com/Attack.php

 

here is the entire code for the page

 

<?php

mysql_connect("localhostl", "username", "password") or die(mysql_error());
mysql_select_db("members") or die(mysql_error());

session_start();

if(!isset($_GET['NPC']))
{
echo "You have not selected a NPC to fight! Please select one from below <br><br>";

$NPCS = mysql_query("SELECT * FROM `npc`");
while($Show = mysql_fetch_array($NPCS))
	{
	echo "<a href=\"Attack.php?NPC=" . $Show['npcname'] . "\">" . $Show['npcname'] . "</a><br>";
	}
exit();
}

$NPC = mysql_escape_string($_GET['NPC']);

echo "<a href=\"Attack?NPC=" . $NPC . "&Action=Attack\">Attack</a><br>
<a href=\"Attack?NPC=" . $NPC . "&Action=Run\">Run</a><br>";

$username = $_COOKIE['ID_my_site'];

if(isset($_COOKIE['NPC']))
{
$UserS = mysql_query("SELECT * FROM `users` WHERE username='$username'");
$UserS = mysql_fetch_array($UserS);
$UserH = $_COOKIE['health'];
$UserA = $UserS['skill'];
$UserA *= .1;

$NPCS = mysql_query("SELECT * FROM `npc` WHERE npcname='$NPC'");
$NPCS = mysql_fetch_array($NPCS);
$NPCH = $_COOKIE['NPCH'];
$NPCA = $NPCS['dmg'];
$NPCA *= .1;
$NPCX = $NPCS['skillgained'];

$NPCH -= $UserA;

if($NPCH <= 0)
	{
	echo "You win and have gained " . $NPCX . " skill";

	$UserA = $UserS['skill'] + $NPCX;

	mysql_query("UPDATE `users`
	SET skill='$UserA'
	WHERE username='$username'");

	unset($_COOKIE['NPC']);

	exit();
	}

setcookie('NPCH', $NPCH, 2400);

$UserH -= $NPCA;

if($UserH <= 0)
	{
	echo "You have lost";

	$UserA = $UserA * .95;

	mysql_query("UPDATE `users`
	SET skill='$UserA'
	WHERE username='$username'");

	unset($_COOKIE['NPC']);

	exit();
	}

setcookie('health', $UserH, 2400);
}
else
{
$UserS = mysql_query("SELECT * FROM `users` WHERE username='$username'");
$UserS = mysql_fetch_array($UserS);

$NPCS = mysql_query("SELECT * FROM `npc` WHERE npcname='$NPC'");
$NPCS = mysql_fetch_array($NPCS);

setcookie('NPC', $NPC, 2400);
setcookie('health', $UserS['health'], 2400);
setcookie('skill', $UserS['skill'], 2400);
setcookie('NPCH', $NPCS['health'], 2400);
}

?>


</body>
</html>



try this

 

<?php

mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("members") or die(mysql_error());

session_start();

if(!isset($_GET['NPC']))
   {
   echo "You have not selected a NPC to fight! Please select one from below <br><br>";

   $NPCS = mysql_query("SELECT * FROM `npc`");
   while($Show = mysql_fetch_array($NPCS))
      {
      echo "<a href=\"Attack.php?NPC={$Show['npcname']}\">{$Show['npcname']}</a><br>";
      }
   exit();
   }

$NPC = mysql_real_escape_string($_GET['NPC']);

echo "<a href=\"Attack.php?NPC=" . $NPC . "&Action=Attack\">Attack</a><br>
<a href=\"Attack.php?NPC=" . $NPC . "&Action=Run\">Run</a><br>";

$username = $_COOKIE['ID_my_site'];

if(isset($_COOKIE['NPC']))
   {
   $UserSQuery = mysql_query("SELECT * FROM `users` WHERE username='$username'");
   $UserS = mysql_fetch_array($UserSQuery);
   $UserH = $_COOKIE['health'];
   $UserA = $UserS['skill'];
   $UserA *= .1;

   $NPCSQuery = mysql_query("SELECT * FROM `npc` WHERE npcname='$NPC'");
   $NPCS = mysql_fetch_array($NPCSQuery);
   $NPCH = $_COOKIE['NPCH'];
   $NPCA = $NPCS['dmg'];
   $NPCA *= .1;
   $NPCX = $NPCS['skillgained'];

   $NPCH -= $UserA;

   if($NPCH <= 0)
      {
      echo "You win and have gained " . $NPCX . " skill";

      $UserA = $UserS['skill'] + $NPCX;

      mysql_query("UPDATE `users`
      SET skill='$UserA'
      WHERE username='$username'");

      unset($_COOKIE['NPC']);

      exit();
      }

   setcookie('NPCH', $NPCH, 2400);

   $UserH -= $NPCA;

   if($UserH <= 0)
      {
      echo "You have lost";

      $UserA = $UserA * .95;

      mysql_query("UPDATE `users`
      SET skill='$UserA'
      WHERE username='$username'");

      unset($_COOKIE['NPC']);

      exit();
      }

   setcookie('health', $UserH, 2400);
   }
else
   {
   $UserSQuery = mysql_query("SELECT * FROM `users` WHERE username='$username'");
   $UserS = mysql_fetch_array($UserSQuery);

   $NPCSQuery = mysql_query("SELECT * FROM `npc` WHERE npcname='$NPC'");
   $NPCS = mysql_fetch_array($NPCSQuery);

   setcookie('NPC', $NPC, 2400);
   setcookie('health', $UserS['health'], 2400);
   setcookie('skill', $UserS['skill'], 2400);
   setcookie('NPCH', $NPCS['health'], 2400);
   }

?>


</body>
</html>

 

Made a lot of little changes!! :)

Try and run this - when you click "Attack" which number appears?

<?php

mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("members") or die(mysql_error());

session_start();

if(!isset($_GET['NPC']))
   {
echo "1";
   echo "You have not selected a NPC to fight! Please select one from below <br><br>";

   $NPCS = mysql_query("SELECT * FROM `npc`");
   while($Show = mysql_fetch_array($NPCS))
      {
      echo "<a href=\"Attack.php?NPC={$Show['npcname']}\">{$Show['npcname']}</a><br>";
      }
   exit();
   }

$NPC = mysql_real_escape_string($_GET['NPC']);

echo "<a href=\"Attack.php?NPC=" . $NPC . "&Action=Attack\">Attack</a><br>
<a href=\"Attack.php?NPC=" . $NPC . "&Action=Run\">Run</a><br>";

$username = $_COOKIE['ID_my_site'];

if(isset($_COOKIE['NPC']))
   {
echo "2";
   $UserSQuery = mysql_query("SELECT * FROM `users` WHERE username='$username'");
   $UserS = mysql_fetch_array($UserSQuery);
   $UserH = $_COOKIE['health'];
   $UserA = $UserS['skill'];
   $UserA *= .1;

   $NPCSQuery = mysql_query("SELECT * FROM `npc` WHERE npcname='$NPC'");
   $NPCS = mysql_fetch_array($NPCSQuery);
   $NPCH = $_COOKIE['NPCH'];
   $NPCA = $NPCS['dmg'];
   $NPCA *= .1;
   $NPCX = $NPCS['skillgained'];

   $NPCH -= $UserA;

   if($NPCH <= 0)
      {
      echo "You win and have gained " . $NPCX . " skill";

      $UserA = $UserS['skill'] + $NPCX;

      mysql_query("UPDATE `users`
      SET skill='$UserA'
      WHERE username='$username'");

      unset($_COOKIE['NPC']);

      exit();
      }

   setcookie('NPCH', $NPCH, 2400);

   $UserH -= $NPCA;

   if($UserH <= 0)
      {
      echo "You have lost";

      $UserA = $UserA * .95;

      mysql_query("UPDATE `users`
      SET skill='$UserA'
      WHERE username='$username'");

      unset($_COOKIE['NPC']);

      exit();
      }

   setcookie('health', $UserH, 2400);
   }
else
   {
echo "3";
   $UserSQuery = mysql_query("SELECT * FROM `users` WHERE username='$username'");
   $UserS = mysql_fetch_array($UserSQuery);

   $NPCSQuery = mysql_query("SELECT * FROM `npc` WHERE npcname='$NPC'");
   $NPCS = mysql_fetch_array($NPCSQuery);

   setcookie('NPC', $NPC, 2400);
   setcookie('health', $UserS['health'], 2400);
   setcookie('skill', $UserS['skill'], 2400);
   setcookie('NPCH', $NPCS['health'], 2400);
   }

?>


</body>
</html>

Now we can see where the code is running - this is telling me that the cookie is NOT set otherwise we'd get "2" appearing and some other form of output.

 

If you look inside the "3" section you can see you're not sending anything to the browser so in effect it is doing what it's told.

 

What should the cookie "NPC" contain?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.