Help with Error from a simple Create databasetable.

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
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Help with Error from a simple Create databasetable.

Post by bironeb »

Below is the code for the html page:

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>
Below is the PHP code to set up the table:

Code: Select all

<?php 
if ((!$_POST[table_name]) || (!$_POST[num_fields])) {
	header("Location: http://127.0.0.1/testing/show_createtable.html");
	exit;
}
$form_block = "
<FORM METHOD="POST" ACTION="do_createtable.php">
<INPUT TYPE="hidden" NAME="table_name" VALUE="$_POST[table_name]">
<TABLE CELLSPACING=5 CELLPADDING=5>
<TR>
<TH>FIELD NAME</TH><TH>FIELD TYPE</TH><TH>FIELD LENGTH</TH></TR>";
for ($i=0; $i < $_POST[num_fields]; $i++) {
	$form_block .="
	<TR>
	<TD ALIGN=CENTER><INPUT TYPE="text" NAME="field_name[]" SIZE="30"></TD>
	<TD ALIGN=CENTER>
	<SELECT NAME = "field_type[]">
		<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[]" SIZE="5"></TD>
	</TR>";
}
$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 "$_POST[table_name]"; ?></H1>
<? echo "$form_block"; ?>
</body>
</html>
Below is the Error:

Notice: Use of undefined constant table_name - assumed 'table_name' on line 2

Notice: Use of undefined constant num_fields - assumed 'num_fields' on line 2

Notice: Use of undefined constant num_fields - assumed 'num_fields' on line 12

Notice: Use of undefined constant num_fields - assumed 'num_fields' on line 12

Notice: Use of undefined constant num_fields - assumed 'num_fields' on line 12

Notice: Use of undefined constant num_fields - assumed 'num_fields' on line 12

Notice: Use of undefined constant num_fields - assumed 'num_fields' on line 12

Notice: Use of undefined constant num_fields - assumed 'num_fields' on line 12

Notice: Use of undefined constant num_fields - assumed 'num_fields' on line 12

Notice: Use of undefined constant num_fields - assumed 'num_fields' on line 12


Any Ideas?
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: Help with Error from a simple Create databasetable.

Post by TheBentinel.com »

bironeb wrote:if ((!$_POST[table_name]) || (!$_POST[num_fields])) {
Wrap table_name and identifiers like that in quotes:

Code: Select all

if ((!$_POST['table_name']) || (!$_POST['num_fields'])) {
And the other ones as well. See if that takes care of it.
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

I changed the php code to the below:

Code: Select all

<?php
if ((!$_POST['table_name']) || (!$_POST['num_fields'])) {
	header("Location: http://127.0.0.1/testing/show_createtable.html");
	exit;
}
$form_block = "
<FORM METHOD="POST" ACTION="do_createtable.php">
<INPUT TYPE="hidden" NAME="table_name" VALUE="$_POST['table_name']">
<TABLE CELLSPACING=5 CELLPADDING=5>
<TR>
<TH>FIELD NAME</TH><TH>FIELD TYPE</TH><TH>FIELD LENGTH</TH></TR>";
for ($i=0; $i < $_POST['num_fields']; $i++) {
	$form_block .="
	<TR>
	<TD ALIGN=CENTER><INPUT TYPE="text" NAME="field_name[]" SIZE="30"></TD>
	<TD ALIGN=CENTER>
	<SELECT NAME = "field_type[]">
		<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[]" SIZE="5"></TD>
	</TR>";
}
$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 "$_POST['table_name']"; ?></H1>
<? echo "$form_block"; ?>
</body>
</html>
And now I get a different error:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING on line 8

Any Idea what this one might be?
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

bironeb wrote:I changed the php code to the below:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING on line 8

Any Idea what this one might be?
No, but my quick research suggests it's a misplaced $ or ", though I couldn't find one in your code.

Best idea would be to reduce the code. Take that first multi-line string and reduce it to just "hi" or something small like that. See if the error goes away. If it does, then your error is in there. If it doesn't, put it back and do it again on the next block.

I hope it helps!
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

try changing this line:

Code: Select all

<INPUT TYPE="hidden" NAME="table_name" VALUE="$_POST['table_name']">
to:

Code: Select all

<INPUT TYPE="hidden" NAME="table_name" VALUE="{$_POST['table_name']}">
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

Well I figured it out... I had to remove the ' that i added in some of the other lines.

Code: Select all

<?php
<INPUT TYPE="hidden" NAME="table_name" VALUE="$_POST[table_name]">
?>
and

Code: Select all

<?php
<H1>Define fields for <? echo "$_POST[table_name]"; ?></H1>


?>
i removed the ' ' around the table_name and it works great now.

Thanks.
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

bironeb wrote:Well I figured it out... I had to remove the ' that i added in some of the other lines.
I keep telling people that there's nothing to be gained by listening to me, but do they listen?

Sorry I misled you, glad you got it fixed! Though I must admit, I don't quite understand why it works without the quotes.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

TheBentinel, you were right in tellign himt o do that. Bironeb, I suggest putting the quotes back in there and then go back and read my previous post and do what it suggests!
Post Reply