Quick easy error, php xperts help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

I3lade
Forum Commoner
Posts: 70
Joined: Wed Jun 04, 2003 4:20 pm

Quick easy error, php xperts help!

Post by I3lade »

Please help me fix this quick error:

Code: Select all

<? 
session_start(); 
if($havepet==1)&#123; 
?> 
 
You already have a pet, you cannot create another!
 
<? 
&#125;else&#123; 
printf("<?
 mysql_connect("localhost","bladegames", "****"); 
 mysql_select_db("bladegames"); 
 session_start();
 $id=$row->userid;
 session_register("$userid");
 if($submit=="Create my pet")
&#123;
mysql_query("UPDATE bb1_user_table SET petname = '$petname', pet = '$pet', pic = '$pic', hp = '10', maxhp = '10', str = '7', def = '5', speed = '8', lv = '1', intl = '6'
WHERE userid = '$user_id'");

 
&#125;

You have choosen Belron!
Belron is a Crocodile-like ZeoPet. Enter/View some information below about your pet:


    <form method=post action="Belron.php">
   </head>
  
   <b>
    <br>
     Pet Name
     <br>
    </b>
    <input type=text size=15 name="petname">
     <br>
      <b>
    <br>
     Pet Type
     <br>
    </b>

 <select name="pic">

        <option value="Belron.bmp">Belron</option>
    </select>
    <br>
    <b>
    <br>
     Pet Color
     <br>
    </b>
<select name="pet">
<option value="Belron">Green</option>
</select>
    <br>


    <input type=submit name=submit value="Create my pet">
    <input type=reset  name=reset  value="Clear">
    <br><br>
    
   </form>  
<br><br>
<hr>
<font color="red" size=3><center><u>Belron's Stats</u></center></font><br>
<font color="black">Level:<font color="green">1<br>
<font color="black">Hp:<font color="green">10<br>
<font color="black">Strength:<font color="green">7<br>
<font color="black">Defense:<font color="green">5<br>
<font color="black">Speed:<font color="green">8<br>
");  
&#125;  
?>
I think it's the printf("<? but don't know how to fix it , if you could paste the code with the correction in it, thanks.
I3lade
Forum Commoner
Posts: 70
Joined: Wed Jun 04, 2003 4:20 pm

Post by I3lade »

Please help, thanks.
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

This should work fine. A few notes:
1. If you are going to be outputting a lot of HTML, it is usually easier to exit/re-enter php instead of doing an echo/print.
2. If you are going to be printing a lot, check out the heredoc syntax for strings (in the manual somewhere)
3. You cannot print mysql commands or PHP commands if you expect them to work.

Code: Select all

<?
print "mysql_connect()";  // this just outputs the string "mysql_connect()" to the browser
mysql_connect(); // this will actually run
?>
4. Make sure you are using the proper session/form variables for the version of php you are using. PHP >4.1.0 uses superglobals, such as $_SESSION[] and $_POST[]. Side note: I strongly suggest using the $_POST[] or $HTTP_POST_VARS[] when accessing post form variables
5. session_start() must be at the top of your script, or it will not work
6. I suggest learning how to use cascading style sheets (CSS) for your HTML. It makes your code much better and also permits easier modification in the future.
7. If register_globals is off in php.ini, you cannot use session_register. You will have to use $_SESSION[] or $HTTP_SESSION_VARS[] (depending upon your PHP version

Enjoy

Code: Select all

<?session_start();
if($havepet==1){

echo "You already have a pet, you cannot create another!";

}else{

mysql_connect("localhost","bladegames", "****");
mysql_select_db("bladegames");
$id=$row->userid;
// if you are using PHP >4.1, do not use session_register.  Do a $_SESSION['userid']=$userid
// If you are using PHP<4.1, use session_register('userid').  The var name, not the $var itself
session_register("userid"); 
if($submit=="Create my pet")
{
  mysql_query("UPDATE bb1_user_table SET petname = '$petname', pet = '$pet', pic = '$pic', hp = '10', maxhp = '10', str = '7', def = '5', speed = '8', lv = '1', intl = '6' WHERE userid = '".$user_id."'"); // variables inside single quotes do not get evaluated. '$var' will not work.  You need to concatenate the strings.
}
?>
You have choosen Belron!
Belron is a Crocodile-like ZeoPet. Enter/View some information below about your pet:

<form method=post action="Belron.php">
<b><br>Pet Name<br></b>
<input type=text size=15 name="petname">
<br><b>
<br>Pet Type<br></b>

<select name="pic">
  <option value="Belron.bmp">Belron</option>
</select>
<br><b>
<br> Pet Color<br></b>
<select name="pet">
  <option value="Belron">Green</option>
</select>
<br>
<input type="submit" name="submit" value="Create my pet">
<input type="reset"  name="reset"  value="Clear">
<br><br>
</form> 

<br><br>
<hr>
<font color="red" size=3><center><u>Belron's Stats</u></center></font><br>
<font color="black">Level:</font><font color="green">1</font><br>
<font color="black">Hp:</font><font color="green">10</font><br>
<font color="black">Strength:</font><font color="green">7</font><br>
<font color="black">Defense:</font><font color="green">5</font><br>
<font color="black">Speed:</font><font color="green">8</font><br>
}
I3lade
Forum Commoner
Posts: 70
Joined: Wed Jun 04, 2003 4:20 pm

Post by I3lade »

so it was the printf? Now im getting a error on line 24 which is You have choosen belron! whats wrong with that? Or on line 54 if i put in what you told me...
I3lade
Forum Commoner
Posts: 70
Joined: Wed Jun 04, 2003 4:20 pm

Post by I3lade »

Parse error: parse error in /data/members/free/tripod/uk/z/e/o/zeopets/htdocs/pages/pets/Belron.php on line 54
You said it should work fine, but its not.
negblo
Forum Newbie
Posts: 19
Joined: Fri Jun 06, 2003 1:17 pm

Post by negblo »

try putting your ?> right before the "You have chosen Belron" instead of at the bottom of the page... reason being that starting with the "you have chosen" part is all html for the rest of the page....



Welkies


negblo :D
negblo
Forum Newbie
Posts: 19
Joined: Fri Jun 06, 2003 1:17 pm

Post by negblo »

PS

you need to remove the } symbol at the bottom as well...


welkies
I3lade
Forum Commoner
Posts: 70
Joined: Wed Jun 04, 2003 4:20 pm

Post by I3lade »

Still got an error...
I3lade
Forum Commoner
Posts: 70
Joined: Wed Jun 04, 2003 4:20 pm

Post by I3lade »

ok so i have :
even if i take out the } it still dosnt work

Code: Select all

<?session_start(); 
if($havepet==1)&#123; 

echo "You already have a pet, you cannot create another!"; 

&#125;else&#123; 

mysql_connect("localhost","bladegames", "****"); 
mysql_select_db("bladegames"); 
$id=$row->userid; 
// if you are using PHP >4.1, do not use session_register.  Do a $_SESSION&#1111;'userid']=$userid 
// If you are using PHP<4.1, use session_register('userid').  The var name, not the $var itself 
session_register("userid"); 
if($submit=="Create my pet") 
&#123; 
  mysql_query("UPDATE bb1_user_table SET petname = '$petname', pet = '$pet', pic = '$pic', hp = '10', maxhp = '10', str = '7', def = '5', speed = '8', lv = '1', intl = '6' WHERE userid = '".$user_id."'"); // variables inside single quotes do not get evaluated. '$var' will not work.  You need to concatenate the strings. 
&#125; 
?> 
You have choosen Belron! 
Belron is a Crocodile-like ZeoPet. Enter/View some information below about your pet: 

<form method=post action="Belron.php"> 
<b><br>Pet Name<br></b> 
<input type=text size=15 name="petname"> 
<br><b> 
<br>Pet Type<br></b> 

<select name="pic"> 
  <option value="Belron.bmp">Belron</option> 
</select> 
<br><b> 
<br> Pet Color<br></b> 
<select name="pet"> 
  <option value="Belron">Green</option> 
</select> 
<br> 
<input type="submit" name="submit" value="Create my pet"> 
<input type="reset"  name="reset"  value="Clear"> 
<br><br> 
</form> 

<br><br> 
<hr> 
<font color="red" size=3><center><u>Belron's Stats</u></center></font><br> 
<font color="black">Level:</font><font color="green">1</font><br> 
<font color="black">Hp:</font><font color="green">10</font><br> 
<font color="black">Strength:</font><font color="green">7</font><br> 
<font color="black">Defense:</font><font color="green">5</font><br> 
<font color="black">Speed:</font><font color="green">8</font><br> 
&#125;
negblo
Forum Newbie
Posts: 19
Joined: Fri Jun 06, 2003 1:17 pm

Post by negblo »

okay try this then.... just copy and paste

Code: Select all

<?
session_start(); 
if($havepet==1)&#123; 
echo "You already have a pet, you cannot create another!"; 
&#125;else&#123; 
mysql_connect("localhost","bladegames", "****"); 
mysql_select_db("bladegames"); 
$id=$row->userid; 
// if you are using PHP >4.1, do not use session_register.  Do a $_SESSION&#1111;'userid']=$userid 
// If you are using PHP<4.1, use session_register('userid').  The var  name, not the $var itself 

session_register("userid"); 
if($submit=="Create my pet") &#123; 
mysql_query("UPDATE bb1_user_table SET petname = '$petname', pet = '$pet', pic = '$pic', hp = '10', maxhp = '10', str = '7', def = '5', speed = '8', lv = '1', intl = '6' WHERE userid= '".$user_id."'"); 

// variables inside single quotes do not get evaluated. '$var' will not work.  You need to concatenate the strings. 
     
&#125;

&#125; //13lade you never closed the first IF statement 
?> 

You have choosen Belron! 
Belron is a Crocodile-like ZeoPet. Enter/View some information below about your pet: 

<form method=post action="Belron.php"> 
     <b><br>Pet Name<br></b> 
     <input type=text size=15 name="petname"> 
     <br>
     <b><br>Pet Type<br></b> 

     <select name="pic"> 
          <option value="Belron.bmp">Belron</option> 
     </select> 

     <br><b> 
     <br> Pet Color<br></b> 

     <select name="pet"> 
          <option value="Belron">Green</option> 
     </select> 

     <br> 
     
     <input type="submit" name="submit" value="Create my pet"> 
     <input type="reset"  name="reset"  value="Clear"> 
     
     <br>
     <br> 
</form> 

<br>
<br> 

<hr> 
     <font color="red" size=3>
      <center>
       <u>Belron's Stats</u>
      </center>
     </font>
</hr>  <!-- 13lade you never closed your header either -->

<br> 
<font color="black">Level:</font><font color="green">1</font><br> 
<font color="black">Hp:</font><font color="green">10</font><br> 
<font color="black">Strength:</font><font color="green">7</font><br> 
<font color="black">Defense:</font><font color="green">5</font><br> 
<font color="black">Speed:</font><font color="green">8</font><br>
That should get you where you need to go... take a look at where i made comments for you so you can see the areas that needed to be fixed
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

</hr> <!-- 13lade you never closed your header either -->
<hr /> (horizontal rule) is a tag without contents, same as <br />.
Closing it is very well, but not <hr>...</hr>, just <hr />
e.g.

Code: Select all

</form>
<br /><br />
<hr />
<font color="red" size=3>
	<center>
		<u>Belron's Stats</u>
	</center>
</font>
negblo
Forum Newbie
Posts: 19
Joined: Fri Jun 06, 2003 1:17 pm

Post by negblo »

doooh!!!

im an idiot.. i wasnt paying attention... glanced at it real quick and my brain processed it as an <h1>

my bad... did the rest of work????
I3lade
Forum Commoner
Posts: 70
Joined: Wed Jun 04, 2003 4:20 pm

Post by I3lade »

Yes, but now havpet is set to 1 for me and it shows the }else{ whats wrong now? Maby the feild havepet isnt suppsoed to be int? I don't know help please!
I3lade
Forum Commoner
Posts: 70
Joined: Wed Jun 04, 2003 4:20 pm

Post by I3lade »

If you could help me fix this i would appreciate it :) Thanks ahead of time
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

Sorry, could you refresh this thread and post your current error messages, I've lost the plot on this one ;)

Regards,
[Edit: Where are you setting $havepet ?]
Post Reply