Notice: Undefined variable: [b]HELP!!![/b]

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

Post Reply
User avatar
ethelpoo
Forum Newbie
Posts: 5
Joined: Wed Aug 07, 2002 11:45 am

Notice: Undefined variable: [b]HELP!!![/b]

Post by ethelpoo »

Hi everybody,

I am just learning php, so it may be obvious to most of you, but I am getting 2 - not so good- messages:

Code: Select all

Notice: Undefined variable: table_name in F:\Apache Group\Apache2\htdocs\test\do_showfielddef.php on line 2

Warning: Cannot add header information - headers already sent by (output started at F:\Apache Group\Apache2\htdocs\test\do_showfielddef.php:2) in F:\Apache Group\Apache2\htdocs\test\do_showfielddef.php on line 4
as you can see I am running Apache 2.0.39, mysql and php4 in windows 2K. Here are lines 1 thru 4:

Code: Select all

<?php
if ((!$table_name) || (!$num_fields)) &#123;
	header("Location: http://localhost/test/show_createtable.html");
	exit;
&#125;
In this process I started with an html form, then created a PHP script that takes information from that form and dynamically creates another form.

Thanks in advance for any help you can give me...

Rosana
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

Your first error is that $table_name doesn't exist. Try changing it to $_REQUEST['table_name'] and $_REQUEST['num_fields'] (see this thread). The second error was caused by the first. The first error triggered the error output message, and since header() won't work after something has been outputted, it gave an error as well.
User avatar
ethelpoo
Forum Newbie
Posts: 5
Joined: Wed Aug 07, 2002 11:45 am

Post by ethelpoo »

Thank you so much for your response. I made the changes to my script and read the string that you suggested, but now I get a different set of errors. I am following the examples from the 'PHP fast and easy' book, which probably needs to be updated. Here is the html and php codes --

Code: Select all

<HTML>
<HEAD>
<TITLE>Create a Database Table: Step 1</TITLE>
</HEAD>
<BODY>

<H1>Step 1: Name and Number</H1>

<FORM METHOD="POST" ACTION="do_showfielddef.php">

<P><strong>Table Name:</strong><br>
<INPUT TYPE="text" NAME="table_name" SIZE=30></p>

<P><strong>Number of Fields:</strong><br>
<INPUT TYPE="text" NAME="num_fields" SIZE=5></p>

<P><INPUT TYPE="submit" NAME="submit" VALUE="Go to Step 2"></p>

</FORM>

</BODY>
</HTML>
and

Code: Select all

<?php
if (($_REQUEST&#1111;'table_name']) or ($_REQUEST&#1111;'num_fields'])) &#123;
	header( "Location: http://localhost/test/show_createtable.html");
	exit;
&#125;

$form_block = "

<FORM METHOD="POST" ACTION="do_createtable.php">
<INPUT TYPE="hidden" NAME="table_name" VALUE="table_name">

<TABLE CELLSPACING=5 CELLPADDING=5>
<TR>
<TH>FIELD NAME</TH><TH>FIELD TYPE</TH><TH>FIELD LENGTH</TH></TR>

";

// create form fields on the fly

for ($i = 0 ; $i <$num_fields; $i++) &#123;

	$form_block .= "
	<TR>
	
	<TD ALIGN=CENTER><INPUT TYPE="text" NAME="field_name&#1111;]" SIZE="30"></TD>

	<TD ALIGN=CENTER>
	<SELECT NAME="field_type&#1111;]">
		<OPTION VALUE="char">char</OPTION>	
		<OPTION VALUE="date">date</OPTION>
		<OPTION VALUE="float">float</OPTION>
		<OPTION VALUE="int">int</OPTION>
		<OPTION VALUE="text">text</OPTION>
		<OPTION VALUE="varchar">varchar</OPTION>
	</SELECT>
	</TD>

	<TD ALIGN=CENTER><INPUT TYPE="text" NAME="field_length&#1111;]" SIZE="5"></TD>
	
	</TR>
	";
	
&#125;


$form_block .= "
<TR>
<TD ALIGN=CENTER COLSPAN=3><INPUT TYPE="submit" VALUE="Create Table"></TD>
</TR>

</TABLE>
</FORM>   
";
?>
<HTML>
<HEAD>
<TITLE>Create a Database Table: Step 2</TITLE>
</HEAD>
<BODY>

<H1>Define fields for<?echo  "table_name";?></H1>

<?echo "$form_block";?>
</BODY>
</HTML>
the error which I now get is:
Notice: Undefined variable: num_fields in F:\Apache Group\Apache2\htdocs\test\do_showfielddef.php on line 20

Define fields fortable_name

I can see that table_name and num_fields have never been defined-- so I guess my question is -- where should I define my values, since the first thing that the php script needs to do is check that the values were actually entered for $table_name and $num_fields?

thanks again for your help
rosana
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

Well, wherever you see $table_name or $num_fields in your script, replace it with $_REQUEST['table_name'] and $_REQUEST['num_fields'] . $_POST['table_name'] and $_POST['num_fields'] would also work, since you submitted the information with the post method. The book was written before PHP 4.2 , which turns off register_globals by default. With register_globals off, you must access a submitted value by $_REQUEST['varname'] instead of $varname .
User avatar
ethelpoo
Forum Newbie
Posts: 5
Joined: Wed Aug 07, 2002 11:45 am

Post by ethelpoo »

It worked!!! The transition from graphic designer to 'developer wanna be' is a bit rocky.

thanks again RandomEngy
I appreciate your time and patience
:D
rosana
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

When you are working with stuff submitted from forms using post it's best practise to use $_POST in favour of $_REQUEST. There's a bit in the manual that describes these predefined variables. The change from register_globals on to off is being sold as a security thing. Using $_REQUEST which contains all the information from $_POST, $_GET, $_COOKIE and $_FILES removes any security benefit.

The best way to do it is to use the array that equates to the data you're trying to retrieve, $_POST for post data, $_GET for get data, $_COOKIE for cookie data etc. That way you can be sure of where the user has sent the data from.

Mac
User avatar
ethelpoo
Forum Newbie
Posts: 5
Joined: Wed Aug 07, 2002 11:45 am

Thank you

Post by ethelpoo »

Thank you twigletmac. I appreciated the explanation; it makes sense to do it right, even in a testing environment.
Post Reply