Page 1 of 1

Problem with attack

Posted: Fri Jan 02, 2015 6:25 pm
by dakar
Hi.
I have problem with attack.
I dont see monsters name.
I'm sorry to bother you, but just getting started

ERROR
Notice: Undefined variable: newcreaturephp in C:\wamp\www\attack.php on line 49
Notice: Undefined variable: creatureinfo2 in C:\wamp\www\attack.php on line 16
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\wamp\www\attack.php on line 16


<?php
include 'connect.php';




$playerinfo="SELECT * from players where name='player1'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);

if (isset($_GET['creature']))
{
$creature=$_GET['creature'];
$creatureinfo="SELECT * from creatures where name = '$creature'";
$createinfo2=mysql_query($creatureinfo) or die("could not get the creature you were fighting!");
$creatureinfo3=mysql_fetch_array($creatureinfo2);

}
else
{
echo "<a href='battle.php>No Creature selected. Go back";
}

$playerhp = $playerinfo3['hpoints'];
$playerattack = $playerinfo3['attack'];
$playerdefense = $playerinfo3['defense'];

$creature = $creatureinfo3['name'];
$creaturehp = $creatureinfo3['hpoints'];
$creatureattack = $creatureinfo3['attack'];
$creaturedefense = $creatureinfo3['defense'];


///////////////////////////players turn////////////////////////

echo "<u> " . $playerinfo3['name'] . "'S Attack</u><br>";
$playerattack = rand(1,20) + $playerattack;
$creaturedefense = rand(1,20) + $creaturedefense;

echo $playerinfo3['name'] . "'s Attack roll is " . $playerattack . "<br>";
echo $creature . "'s defense roll is " . $creaturedefense. "<br>";

if ($playerattack > $creaturedefense)
{
echo $playerinfo3['name'] . "hits! <br>";
$playerdamage = rand(1,6);
$newcreaturehp = $creaturehp - $playerdamage;
echo "For " . $playerdamage . " points of damage. <br>";
if ($newcreaturephp < 1)
{
echo "The " . $creature . " has been killed";
$updatecreature="DELETE from creatures where name='$creature' limit 1";
mysql_query($updatecreature) or die("Could not update creature");
echo "<a href='battle.php>Go Back";
exit;
}
$updatecreature="update creatures set hpoints='$newcreaturehp' where name='$creature'";
mysql_query($updatecreature) or die("Could not update creature");
}
else
{
echo $playerinfo3['name'] . " misses!<br>";
}
//////////////////////////creatures turn //////////////////////

echo "<u> " . $creature . "'s Attack</u><br>";
$creatureattack = rand(1,20) + $creatureattack;
$playerdefense = rand(1,20) + $playerdefense;

echo $creature . "'s Attack roll is " . $creatureattack . "<br>";
echo $playerinfo3['name'] . "'s defense roll is " . $playerdefense . "<br>";

if ($creatureattack > $playerdefense)
{
echo $creature . " hits! <br>";
$creaturedamage = rand(1,6);
$newplayerhp = $playerhp - $creaturedamage;
echo "For " . $creaturedamage . " points of damage. <br>";
if ($newplayerhp < 1)
{
echo $playerinfo3['name'] . " has been killed<br>";
echo "<a href='gameover.php'>Continue";
exit;
}
$updateplayer="update players set hpoints='$newplayerhp' where name='player1'";
mysql_query($updateplayer) or die("could not update player");
}
else
{
echo $creature . " misses!";
}
echo "<br><br><a href='battle.php?creature'=$creature>Battle Again!";
?>

Re: Problem with attack

Posted: Fri Jan 02, 2015 7:14 pm
by KyleMassacre
This would be kind of difficult to explain to a new person and not really understand how this tutorial you are following actually works but I will give it a shot.

Your query is returning null because it isn't finding anything in the database with that creatures name. The creatures name is coming from the $_GET super global variable which comes from the url Which seems to be battle.php?creature=somecreaturenamehere. When you first click the link to go there is there a creatures name in the url at the end? If so, do you have that creature in your database?

Also, can you please use the code button and put your code inside there next time please? It just makes it easier for people to read it and see what's going on Or use [*code*]code goes here[*\code*] without the stars

Re: Problem with attack

Posted: Fri Jan 02, 2015 7:52 pm
by dakar
In battle.php monster name see.
In battle.php?creature dont see.
Screens:

Re: Problem with attack

Posted: Fri Jan 02, 2015 8:03 pm
by hallsofvallhalla
I recommend watching all the way to video 14 then doing after that. The videos are very old and some things are depricated.

It looks as though in the player table the creature on the player is wrong or missing. Check that field to see what is happening.

Re: Problem with attack

Posted: Fri Jan 02, 2015 8:53 pm
by dakar
Table it's okay.
In attack.php errors, why?

Re: Problem with attack

Posted: Fri Jan 02, 2015 10:53 pm
by dakar
I fix it.
There it was misspelled.
$createinfo2 -> $creatureinfo2
.