PHP - HandleForm.php not working [T_Variable Error

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
Labbat
Forum Newbie
Posts: 15
Joined: Mon Jan 26, 2004 9:10 pm

PHP - HandleForm.php not working [T_Variable Error

Post by Labbat »

Hello,

Im making a simple news publishing deal, for my site...
Now, Ive made the Form.html
(So a user can fill it out, then hit submit)
Ive also made the next page 'handleform.php'
Ive provided the code for HandleForm.php below, and form.html...
Form.html, you shouldn't have too look at, I just threw it in there..
However, my question is this:
When I go to the form.html, fill it out, and hit submit, it gives me this error:
Parse error: parse error, unexpected T_VARIABLE in c:\program files\apache group\apache\htdocs\handleform.php on line 19

And Im not sure why it's giving me that error, because I read thru the code, and I found an error, but I did fix it, however, it didn't fix the error...
And just a tip, I have know db_username, or db_password
for any of my Databases...just so you guys know when you see it in the code.
_________________________________________________________
Here is the code for HandleForm.php

<html>
<head>
<Title>Inserting Data</title>
<body>
<?php
/* This page recieves and handles th e data
generated by "form.html". */
// Trim the incoming data.
$Array["FirstName"] = trim
($Array["FirstName"]);
$Array["LastName"] = trim
($Array["LastName"]);
$Array["Email"] = trim
($Array["Email"]);
$Array["Comments"] = trim
($Array["Comments"]);

$Host = "localhost"
$DBName = "New";
$TableName = "Feedback";

$Link = mysql_Connect ($localhost);
$Query = "INSERT into $TableName values
('0', '$Array[FirstName]',
'Array[LastName', 'Array[Email]',
'$Array[Comments]')";
print ("The query is:<BR>$Query<P>\n");
if (mysql_db_query($DBName, $Query,
$Link)) {
print ("The query was successfully
executed!<BR>\n");
} else {
print ("The query could not be
executed<BR>\n");
}
mysql_close($Link);
?>
</body>
</html>

___________________________________________________________

Here is the code for form.html

<HTML>
<HEAD>
<TITLE>HTML Form</title>
</HEAD>
<BODY>
<FORM ACTION="HandleForm.php" METHOD=POST>
First Name <INPUT TYPE=TEXT NAME="Array[FirstName]" SIZE=20><BR>
Last Name <INPUT TYPE=TEXT NAME="Array[LastName]" SIZE=40><BR>
Email Address <INPUT TYPE=TEXT NAME="Array[Email]" SIZE=60><BR>
Comments <TEXTAREA NAME="Array[Comments]" ROWS=5 COLS=40<TEXTAREA></textarea><br>
&nbsp;<INPUT TYPE=SUBMIT NAME="SUBMIT" VALUE="SUBMIT!">
</FORM>
</BODY>
</HTML>
User avatar
Pointybeard
Forum Commoner
Posts: 71
Joined: Wed Sep 03, 2003 7:23 pm
Location: Brisbane, AUS
Contact:

Post by Pointybeard »

Umm...if you looked at line 19 you would see that you are missing a semi-colon

Code: Select all

$Host = "localhost"
$DBName = "New";
$TableName = "Feedback";
That error you got is almost always because you forgot a semi-colon. ;)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You are relying on register_globals being on for your script, since it is now off by default you should be using $_POST (if you are using PHP < 4.1 use $HTTP_POST_VARS) to get information posted via a form.

For example, if you changed the initial form to:

Code: Select all

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;HTML Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action="HandleForm.php" method="post"&gt;
	First name &lt;input type="text" name="Firstname" size="20" /&gt;&lt;br /&gt;
	Last name &lt;input type="text" name="Lastname" size="40" /&gt;&lt;br /&gt;
	Email Address &lt;input type="text" name="Email" size="60" /&gt;&lt;br /&gt;
	Comments &lt;textarea name="Comments" rows="5" cols="40"&gt;&lt;/textarea&gt;&lt;br /&gt;
	&lt;input type="submit" name="submit" VALUE="SUBMIT!" /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
you could then do the following:

Code: Select all

<html>
<head>
<title>Inserting Data</title>
</head>

<body>
<?php

// loop through the $_POST array to trim() all data, makes life
// much easier than doing it element by element
foreach ($_POST as $key => $value) {
	$_POST[$key] = trim($value);
}

$Host = "localhost";
$DBname = "New";
$Tablename = "Feedback";

$Link = mysql_connect($Host);

// you should never plug data directly into MySQL from a form without
// check that data first to ensure that no SQL injection or other
// problems are happening
$Query = "INSERT into $Tablename";
$Query .= "VALUES (0, '$_POST[Firstname]', '$_POST[Lastname]', '$_POST[Email]', '$_POST[Comments]')";

print '<p>The query is:<br />'.$Query.'</p>'."\n";

// mysql_db_query() is a deprecated function and has been for
// some time - you should be using mysql_select_db() and mysql_query()
// instead
@mysql_select_db($DBname) or die(mysql_error());

if (@mysql_query($Query)) {
	print '<p>The query was successfully executed!</p>'."\n";
} else {
	print '<p>The query could not be executed.</p>'."\n";
	print '<p><b>MySQL Error</b>: '.mysql_error().'</br>';
	print '<b>SQL Statement</b>: '.$Query.'</p>'."\n";
}

mysql_close($Link);
?>
</body>
</html>
Mac
Last edited by twigletmac on Tue Feb 03, 2004 2:26 am, edited 1 time in total.
Labbat
Forum Newbie
Posts: 15
Joined: Mon Jan 26, 2004 9:10 pm

Thanks :)

Post by Labbat »

Great Guys, thanks for the help :) I appreciate it :)
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

hey why isnt this throwing up and error

from what i can see you have defined

Code: Select all

$Host = "localhost" 
$DBName = "New"; 
$TableName = "Feedback";
and then you are using this to connect

Code: Select all

$Link = mysql_Connect ($localhost);
when the variable $localhost doesnt exist?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Well spotted (I updated my code snippet to account for my blindness :lol:)

Mac
Post Reply