Need Help with Register and Reguser.[Solved]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Need Help with Register and Reguser.[Solved]

Post by MikeD »

Well I am trying to have players be able to create characters on my game, on a players account.

Everytime I try to make a new character on my account, all I get is "could not register."

No error or anything.

Here's the full code for this page.

I've debugged the error into this code.

Code: Select all

elseif ($type == "bronze" )
{
if ($playerinfo3['credits'] < 1000)
{
 echo "You do not have enough Credits to create this Warrior!";
  echo "<p><center>You can buy more credits <a href='store.php'>Here</a></center></p>";
  exit;
}
else
{
$bronze = 1000;

$SQL = "INSERT into warriors(pid, pname,name, speed,power,magic,range,defense,vision,luck,health,agility,exper,level,mana,type,hpoints,maxhpoints,maxmana,gold) VALUES ('$pid','$pname','$warrior','1','1','1','1','1','1','1','1','1','0','1','10','$type','10','10','10','250')";
mysql_query($SQL) or die("could not register2");

$updateplayer="update players set credits=credits-'$bronze' where name='$player'";
  mysql_query($updateplayer) or die("Could not update player");
} }
Reg user

Code: Select all

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

    $pid=$playerinfo3['userid'];
    $pname=$playerinfo3['name'];
    $credits=$playerinfo3['credits'];
    
    $warrior=$_POST['warrior'];
    $type=$_POST['type'];
    $mage=$_POST['mage'];

if ($warrior == "")
{
echo "<p>You didn't enter a Warrior Name!</p>";
echo "<p> <A href='createwarrior.php'>Go back</a></p>";
exit;
}

if ($type == "")
{
echo "<p>You didn't select the type of Warrior you want to create!</p>";
echo "<p> <A href='createwarrior.php'>Go back</a></p>";
exit;
}

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

if($isplayer3 || strlen($warrior)>25 || strlen($warrior)<1)
{
print "There is already a Warrior of that name or the name you specified is over 25 letters or less than 1 letter";
echo " <A href='createwarrior.php'>Go back</a><br>";
}

if ($type == "free" )
{
$SQL = "INSERT into warriors(pid, pname, name, speed, power, magic, range, defense, vision, luck, health, agility, exper, level, mana, type, hpoints, maxhpoints, maxmana, gold) VALUES ('$pid','$pname','$warrior','1','1','1','1','1','1','1','1','1','0','1','10','$type','10','10','10','100')";
mysql_query($SQL) or die("could not register");
}

elseif ($type == "bronze" )
{
if ($playerinfo3['credits'] < 1000)
{
 echo "You do not have enough Credits to create this Warrior!";
  echo "<p><center>You can buy more credits <a href='store.php'>Here</a></center></p>";
  exit;
}
else
{
$bronze = 1000;

$SQL = "INSERT into warriors(pid, pname, name, speed, power, magic, range, defense, vision, luck, health, agility, exper, level, mana, type, hpoints, maxhpoints, maxmana, gold) VALUES ('$pid','$pname','$warrior','1','1','1','1','1','1','1','1','1','0','1','10','$type','10','10','10','250')";
mysql_query($SQL) or die("could not register");

$updateplayer="update players set credits=credits-'$bronze' where name='$player'";
  mysql_query($updateplayer) or die("Could not update player");
} }

elseif ($type == "silver" )
{
if ($playerinfo3['credits'] < 1800)
{
 echo "You do not have enough Credits to create this Warrior!";
  echo "<p><center>You can buy more credits <a href='store.php'>Here</a></center></p>";
  exit;
}
else
{
$silver = 1800;

$SQL = "INSERT into warriors(pid, pname, name, speed, power, magic, range, defense, vision, luck, health, agility, exper, level, mana, type, hpoints, maxhpoints, maxmana, gold) VALUES ('$pid','$pname','$warrior','2','2','2','2','2','2','2','2','2','0','2','20','$type','20','20','20','500')";
mysql_query($SQL) or die("could not register");

$updateplayer="update players set credits=credits-'$silver' where name='$player'";
  mysql_query($updateplayer) or die("Could not update player");
} }

elseif ($type == "gold" )
{
if ($playerinfo3['credits'] < 3250)
{
 echo "You do not have enough Credits to create this Warrior!";
  echo "<p><center>You can buy more credits <a href='store.php'>Here</a></center></p>";
  exit;
}
else
{
$gold = 3250;

$SQL = "INSERT into warriors(pid, pname, name, speed, power, magic, range, defense, vision, luck, health, agility, exper, level, mana, type, hpoints, maxhpoints, maxmana, gold) VALUES ('$pid','$pname','$warrior','4','4','4','4','4','4','4','4','4','0','4','40','$type','40','40','40','1000')";
mysql_query($SQL) or die("could not register");

$updateplayer="update players set credits=credits-'$gold' where name='$player'";
  mysql_query($updateplayer) or die("Could not update player");
}}

else if ($type == "platinum")
{
if ($playerinfo3['credits'] < 5750)
{
 echo "You do not have enough Credits to create this Warrior!";
  echo "<p><center>You can buy more credits <a href='store.php'>Here</a></center></p>";
  exit;
}
else
{
$plat = 5750;
$SQL = "INSERT into warriors(pid, pname, name, speed, power, magic, range, defense, vision, luck, health, agility, exper, level, mana, type, hpoints, maxhpoints, maxmana, gold) VALUES ('$pid','$pname','$warrior','10','10','10','10','10','10','10','10','10','0','10','100','$type','100','100','100','2500')";
mysql_query($SQL) or die("could not register");

$updateplayer="update players set credits=credits-'$plat' where name='$player'";
  mysql_query($updateplayer) or die("Could not update player");
}}

if ($mage=1)
{
if ($playerinfo3['credits'] < 5000)
{
 echo "You do not have enough Credits for the Mage Boost!";
  echo "<p><center>You can buy more credits <a href='store.php'>Here</a></center></p>";
  exit;
}
else
{
$warriorinfo="SELECT * from warriors where name='$warrior'";
$warriorinfo2=mysql_query($warriorinfo) or die("Could not query players table");
$warriorinfo3=mysql_fetch_array($warriorinfo2);

$magelvl=$warriorinfo3['magic'];
$manalvl=$warriorinfo3['mana'];
$maxmana=$warriorinfo3['maxmana'];

$updatewarrior="update warriors set `$magelvl`=`$magelvl`+10,`$manalvl`=`$manalvl`+100,`$maxmana`=`$maxmana`+100 where name='$warrior'";
  mysql_query($updateplayer) or die("Could not update player");
}}
?>
and the registration page

Code: Select all

    <form action="regwarrior.php" method="POST" />
      <p>Warrior's Name:<input type="text" name="warrior" size="25"></p>
      <p>Player Type <select name="type">
      <option value="free">Free</option>
      <option value="bronze">Bronze (1000 Credits)</option>
      <option value="silver">Silver (1800 Credits)</option>
      <option value="gold">Gold (3250 Credits)</option>
      <option value="platinum">Platinum (5750 Credits)</option></p>
      </select>
      <p>Mage Boost +10 Levels and +100 Mana(5000 Credits)<input type="checkbox" name="mage" value="mage" /></p>
      <p><input type="submit" value="Create" name="create" /></p>
      </form>
Last edited by MikeD on Wed Oct 05, 2011 1:26 pm, edited 2 times in total.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Need Help with Register and Reguser.

Post by Jackolantern »

In the block of code that generates the "could not register" error (the INSERT query to add the new character), you need to remove the single-quotes around the numbers, such as the 1's and any other plain numbers. You only add the single-quotes around strings, such as names.
The indelible lord of tl;dr
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Re: Need Help with Register and Reguser.

Post by MikeD »

Jackolantern wrote:In the block of code that generates the "could not register" error (the INSERT query to add the new character), you need to remove the single-quotes around the numbers, such as the 1's and any other plain numbers. You only add the single-quotes around strings, such as names.
Tried that, still getting the "Could not register2"

Code is now like this

Code: Select all

elseif ($type == "bronze" )
{
if ($playerinfo3['credits'] < 1000)
{
 echo "You do not have enough Credits to create this Warrior!";
  echo "<p><center>You can buy more credits <a href='store.php'>Here</a></center></p>";
  exit;
}
else
{
$bronze = 1000;

$SQL = "INSERT into warriors(pid, pname, name, speed, power, magic, range, defense, vision, luck, health, agility, exper, level, mana, type, hpoints, maxhpoints, maxmana, gold) VALUES ('$pid','$pname','$warrior',1,1,1,1,1,1,1,1,1,0,1,10,'$type',10,10,10,250)";
mysql_query($SQL) or die("could not register2");

$updateplayer="update players set credits=credits-'$bronze' where name='$player'";
  mysql_query($updateplayer) or die("Could not update player");
} }
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Need Help with Register and Reguser.

Post by Jackolantern »

You should be sure to turn on error reporting at the top of your script:

Code: Select all

error_reporting(E_ALL);
Now go right above the INSERT query, and make an ECHO statement that prints out all of the values you are inserting into the database. For the value titles, use the names of the values you are inserting. For example:

Code: Select all

echo "pid: ".$pid."<br />pname: ".$pname."<br /> name: ".$warrior."<br /> speed: 1";
...etc. You can just type the literal numeric values right into the string, but be sure you get them from the SQL INSERT statement. This is called a "variable dump" and it is probably the most used PHP debugging method, which you will start with on almost every problem.

This will show you exactly what you are attempting to insert each time, and often that can show you the problem. Something in that query is causing a problem.
The indelible lord of tl;dr
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Re: Need Help with Register and Reguser.

Post by MikeD »

Variables print just fine

pid: 2
pname: MikeD
name: test
speed: 1 bronzecould not register2

I've double checked the spelling for each insert. Really unsure of what's causing it.
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Need Help with Register and Reguser.

Post by SpiritWebb »

You placed the:

Code: Select all

error_reporting(E_ALL);
right?

Also at the end of could not register2, look like this:

Code: Select all

("Could not register2" . mysql_error());
That way you can see where the error is coming from, and let us know...
Image

Image
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Re: Need Help with Register and Reguser.

Post by MikeD »

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'range, defense, vision, luck, health, agility, exper, level, mana, hpoints, maxh' at line 1
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Need Help with Register and Reguser.

Post by Jackolantern »

Oh snap! I bet you "range" is a MySQL reserved word. Put back-ticks ( ` which is just to the left of the 1 key, right below the Escape key) around all of the names of the columns in your INSERT statement or consider changing the name of that column.

Code: Select all

$SQL = "INSERT into warriors(`pid`, `pname`, `name`, `speed`, `power`, `magic`, `range`, `defense`, `vision`, `luck`, `health`, `agility`, `exper`, `level`, `mana`, `type`, `hpoints`, `maxhpoints`, `maxmana`, `gold`) VALUES ('$pid','$pname','$warrior',1,1,1,1,1,1,1,1,1,0,1,10,'$type',10,10,10,250)";
mysql_query($SQL) or die("could not register2");
I don't think "type" is a reserved word, but I guess it could be in there somewhere, too.

EDIT: Yep, "range" is a reserved word. But "type" is not.

EDIT2: Oh, and just for informative purposes, the reasoning behind using back-ticks is so that you can use reserve words or words that have spaces in them for column names.
The indelible lord of tl;dr
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Re: Need Help with Register and Reguser.

Post by MikeD »

Code: Select all

$SQL = "INSERT into warriors(`pid`, `pname`, `name`, `speed`, `power`, `magic`, `range`, `defense`, `vision`, `luck`, `health`, `agility`, `exper`, `level`, `mana`, `type`, `hpoints`, `maxhpoints`, `maxmana`, `gold`) VALUES ('$pid','$pname','$warrior',1,1,1,1,1,1,1,1,1,0,1,10,'$type',10,10,10,250)";
mysql_query($SQL) or die("could not register2");
I don't think "type" is a reserved word, but I guess it could be in there somewhere, too.

EDIT: Yep, "range" is a reserved word. But "type" is not.[/quote]

This fixed it, you are awesome! :)

Thanks for all the help guys! Truly appreciate it.
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Need Help with Register and Reguser.

Post by SpiritWebb »

you will have to change the word range. Jack is right, although the ticks (`) doesn't matter, the single quote (') works just fine, but that word will have to change!!
Image

Image
Post Reply

Return to “Beginner Help and Support”