Page 1 of 1

Several errors in a PHP file(Following tutorial)

Posted: Fri Nov 16, 2012 9:27 pm
by oshirotg
Hello there! first of all let me say: THANK YOU to indie-resource... for existing :D and to hallsofValhalla for guiding me here :)
I am at his "3b" video in making a browser MMORPG, and I get tons of errors when trying to press the Submit button...
Error: Fatal error: Function name must be a string in D:\Programfiler (x86)\wamp\www\tutorial\reguser.php on line 6
Code:

Code: Select all

<?php
include 'connect.php';
?>

<?php
$players=$_POST('player');
$password=$_POST('password');
$repassword=$_POST('repassword');
$player=strip_tags($player);
$email=$_POST('email');
$email=strip_tags($email);

if ($email == "")
{
echo "Please enter a email address!<br>";
echo " <A href='register.php'>Go back</a>";
exit;
}

if ($password==$repassword)
{

$isplayer="SELECT * from players where name='$player'";
$isplayer2=mysql_query($isplayer) or die("Could not query players table");
$isplayer3=mysql_fetch_array($isplayer2);

if(!$_POST('password') || !$_POST('repassword'))
{
print "Please enter a password<br>";
echo " <A h ref='register.php'>Go back</a>";
exit;
}
else if($isplayer3)
{
print "That username is already in use! Please choose another one.";
echo " <A href='register.php'>Go back</a><br>";
exit;
}

else if(strlen($player)>20 || strlen($player)<3)
{
print "Your username is either too long or too short. It needs to be between 3-20 letters.";
echo " <A href='register.php'>Go back</a><br>";
exit;
}

else
{
$isemail="SELECT * from players where email='$email'";
$isemail2=mysql_query($isemail) or die("Not able to query for email");
$isemail3=mysql_fetch_array($isemail2);
if($isemail3)
{
  print "That E-mail address is already in use!";
  echo "A href='register.php'>Go back</a><br>";
  exit;
}
else
{
$password=md5($password);

$SQL ="INSERT into players(name, password, email, level, xp) VALUES ('$player','$password','1','1')";
mysql_query($SQL) or die("Could not register");

print "Thank you for registering!";

}
}
}
else
{
print "Your password didn't match or you did not enter a password";
echo " <A href='registter.php'>Go back></a><br>";
exit;
}
echo " <A href='login.php'>Click here to log in!</a><br>";
?>

DELETED DELETED DELETED DELETED DELETED

Posted: Fri Nov 16, 2012 10:27 pm
by randomalexlol
DELETED DELETED DELETED DELETED DELETED DELETED DELETED DELETED DELETED DELETED DELETED DELETED

Re: Several errors in a PHP file(Following tutorial)

Posted: Sat Nov 17, 2012 12:45 am
by oshirotg
randomalexlol wrote:There are a few errors you have done, such as putting $_POST('player') instead of $_POST['player'] easy mistake.

Here's how i would code it :)

i use $_REQUEST because it can pick up both $_POST and $_GET so it's good but also bad if you like to have different values sent in the same name to the same place.

i try not to use else{} as much as possible it just makes things way too complicated as soon as you have more than one else, so i just do "if this is wrong, kill everything otherwise continue as normal".

even though the "die;" is within a if(){} it will kill the entire script.

good luck :)

Code: Select all

<?php
    include("connect.php");
    
    if(!$_REQUEST['player'] || $_REQUEST['player']==""){
        //no player name given
        die;
    }
    if(!$_REQUEST['password'] || $_REQUEST['password']==""){
        //no password given
        die;
    }
    if(!$_REQUEST['repassword'] || $_REQUEST['repassword']==""){
        //no 2nd password given
        die;
    }
    if(!$_REQUEST['email'] || $_REQUEST['email']==""){
        //no email given
        die;
    }
    
    $players = strip_tags($_REQUEST['player']);
    $password = md5($_REQUEST['password']);
    $password2 = md5($_REQUEST['repassword']);
    $email = strip_tags($_REQUEST['email']);
    
    if(strlen($players) > 20 || strlen($players) < 3){
        //player name not between 3 and 20
        die;
    }
    
    if(filter_var($email,FILTER_VALIDATE_EMAIL)==false){
        //not a real email address error goes here
        die;
    }
    
    if($password!==$password2){
        //passwords do not match error goes here
        die;
    }
    
    $search_for_player = mysql_query("select * from `players` where `name`='".$player."'");
    $search_for_player_nr = mysql_num_rows($search_for_player);
    if($search_for_player_nr!==0){
        //that player name already exists
        die;
    }
    
    $search_for_email = mysql_query("select * from `players` where `email`='".$email."'");
    $search_for_email_nr = mysql_num_rows($search_for_email);
    if($search_for_email_nr!==0){
        //that email is already in use
        die;
    }
    
    mysql_query("insert into `players` (`name`,`password`,`email`,`level`,`xp`) values ('".$player."','".$password."','".$email."','1','1')");
?>
Thank You For Registering :)

Thank you very much :p I had already fixed this (I'm stuck with another problem though :P) There was tons of minor errors in this one...



But now I'm on video "5" and I'm making the attack.php ...
It worked fine until I killed the goblin... However, the "orc" won't loose hitpoints when I hit him...

Code: Select all

<?php
include 'connect.php';




$playerinfo="SELECT * FROM players WHERE name='Petter'";
$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 creature WHERE name = '$creature'";
$creatureinfo2=mysql_query($creatureinfo) or die("Could not get the creaure you were fighting!");
$creatureinfo3=mysql_fetch_array($creatureinfo2);

}
else
{
 echo "<a href='batle.php>No Creature Selected. Go Back!";
}

$playerhp = $playerinfo3['hitpoint'];
$playeratk = $playerinfo3['attack'];
$playerdef = $playerinfo3['defence'];

$creature = $creatureinfo3['name'];
$creaturehp = $creatureinfo3['hitpoint'];
$creatureatk = $creatureinfo3['attack'];
$creaturedef = $creatureinfo3['defence'];



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

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

echo $playerinfo3['name'] . "'s Attack roll is " . $playeratk . "<br>";
echo $creature . "'sdefence roll is " . $creaturedef . "<br>";

if ($playeratk > $creaturedef)
{
  echo $playerinfo3['name'] . " hits! <br>";
  $playerdamage = rand(1,6);
  $newcreaturehp = $creaturehp - $playerdamage;
  echo "For " . $playerdamage . " points of damage . <br>";
  if($newcreaturehp <1)
  {
    echo "The " . $creature . " has been killed!";

     $updatecreature="DELETE FROM creature 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 hitpoint='$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>";
$creatureatk = rand(1, 20) + $creatureatk;
$playerdef = rand(1, 20) + $playerdef;

echo $creature . "'s Attack roll is " . $creatureatk . "<br>";
echo $playerinfo3['name'] . "'s defence roll is " . $playerdef . "<br>";

if($creatureatk > $playerdef)
{
  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 hitpoint='$newplayerhp' where name='Petter'";
 mysql_query($updateplayer) or die("Could not update player");
}
else
{
  echo $creature . "Misses!";
}
  echo "<form action='battle.php' method='GET'> <input type='submit' value='Stats'> <input type='hidden' name='creature' value='$creature'> </form>";
?>



Any idea why?

Re: Several errors in a PHP file(Following tutorial)

Posted: Sat Nov 17, 2012 1:15 am
by Jackolantern
Our typical advice is to not type anything until you get to at least video 14 or further. This is because you will have seen basic code structures enough by then to be able to spot the $_POST('player') vs. $_POST['player'] issues, and because the game is not in a playable state until much further along. You are going to get a ton of errors if you try to run it way before video 14 because too many systems are missing. You simply won't have a runable program.