Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with php parse error

Status
Not open for further replies.

comboy

Instructor
May 23, 2003
226
Hi all,

Hope some one can help me here I am developing a guestbook for a college project and am getting the following error

Parse error: parse error, unexpected T_VARIABLE, expecting ',' or ';' in C:\Program Files\Apache Group\Apache2\htdocs\guestbook\guestbook.php on line 100

The origional code I'm looking at was written in php 4 but I'm using php and MySQL 5 but don't think that should make much of a difference

I know it more than likely something small but I just can't see it.

Here is my code

Code:
<?php
	require ($_SERVER['DOCUMENT_ROOT']."/guestbook/config/db_config.php");
	
	$connection = mysql_connect($db_host, $db_user, $db_pass) or die("Sorry we can not connect to the database at the moment");

	mysql_select_db ($db_name, $connection);

	$name = $_POST("txt_name");
	$len = strlen ($name);
	if ($len > 0)
	{
	$email = $_POST ("txt_email");
	$comment = $_POST ("txt_comment");
	$date = time ();

	$query = "INSERT INTO guestbook (autoid, name, email, comment, date_auto) VALUES (NULL, '$name', '$email', '$comment', '$date')";
	mysql_query ($query, $connection) or die (mysql_error());
	}
	?>

<html>
<head>
<title>guestbook</title>

</head>

<body>
<table width="70%"  border="0" align="center">
  <tr>
    <td>Welcome to</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><form method="post" action="<?php echo $_SERVER['php_self'];?>">
      <table width="100%"  border="0" align="center" bgcolor="#CCCCCC">
        <tr>
          <td width="13%">Name:</td>
          <td width="87%"><input name="txt_name" type="text" id="txt_name"></td>
        </tr>
        <tr>
          <td>Email:</td>
          <td><input name="txt_email" type="text" id="txt_email"></td>
        </tr>
        <tr>
          <td>Comments:</td>
          <td><textarea name="txt_comment" cols="50" rows="10" id="txt_comment"></textarea></td>
        </tr>
        <tr>
          <td><input type="submit" name="Submit" value="Submit"></td>
          <td>&nbsp;</td>
        </tr>
      </table>
    </form></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>

<p>&nbsp;</P>
 
<table width="70%" border="0" align="center" cellpadding="1" cellspacing="2">
	<?php
	
	$query = "SELECT * FROM guestbook ORDER BY date_auto";
	
	$result = mysql_query ($query, $connection);

	for ($i = 0; $i < mysql_num_rows ($result); $i++)
	{
	$name = mysql_result($result, $i, "name");
	$email = mysql_result($result, $i, "email");
	$email_len = strlen ($email);
	$comment = mysql_result($result, $i, "comment");
	$comment =nl2br ($comment);
	$date = mysql_result($result, $i, "date_auto");
	$show_date = date("H:i:s m/d/Y", $date);
	
	if ($i % 2)
	{
		$bg_color="#ffcc00";
	}
	else
	{
		$bg_color="#fffff";
	}
	
	echo '
	<tr> 
	<td width="100%" bgcolor="'.$bgcolor.'">
	<font face="ariel" size="2">';

	if ($email_len > 0)
	{
	echo '<b> Name: </b> <a href="mailto:.'$email'.'">'.$name.'</a>';
	}
	else
	{
	echo '<b> Name: </b>'.$name;
	}
	echo '<br> 
	<b> Comments: </b>'.$comment.' 
	</font>

	<td>
	
	<td width="1%" valign="top" nowrap bgcolor="'.bg_color.'">
	<font face="ariel" size="2">
	<b> Date: </b>'.$show_date.'
	</font>
	</td>
	</tr>
	';
}

?>	
</table>

</body>
</html>

All help greatly appreciated

Graham
 
Hi all,

I think I have found the part of the code that I'm getting the error on.

Code:
if ($email_len > 0)
	{
	echo '<b> Name: </b> <a href="mailto:.'$email'.'">'.$name.'</a>';
	}
	else
	{
	echo '<b> Name: </b>'.$name;
	}

However I'm not sure on how to fix it.

I know it has to do with the way php reads " and ' but can't figure out where I'm going wrong with it.


Thanks,


Graham.
 
I do believe your error is in this line actually:
Code:
    <td width="1%" valign="top" nowrap bgcolor="'.bg_color.'">
You're calling bg_color which is not defined as a constant anywhere. I do believe you were trying to put $bgcolor there (according to the code above).
 
Hi Vragabond,

I actually seen that typo two minutes ago and changed it but am still getting the same parse error.
Any more ideas? I'm looking at it so long I'm seeing it in my sleep.

Thanks for your help


Graham
 
Code:
echo '<b> Name: </b> <a href="mailto:[red]'.$email.'[/red]">'.$name.'</a>';
 
Hi jpadie,

Thanks that worked but I've now got another error I'll try and see if I can find a fix for it before creating a new post.

Thanks again

Graham
 
have a read of sleipnir214's excellent debugging FAQ on this site. it gives plenty of techniques for resolving issues.
 
Thanks guys

In getting a fatal error: Function name must be a string in C:\Program Files\Apache Group\Apache2\htdocs\guestbook\guestbook.php on line 8

I'll read the faq and see if this will help.

Thanks again

Graham
 
That'll be lines like these:

$name = $_POST("txt_name");

$_POST is an array and the way to reference the elements of an array in PHP is using square brackets, not parentheses. The line should read:

$name = $_POST[red][[/red]"txt_name"[red]][/red];


Want the best answers? Ask the best questions! TANSTAAFL!
 
Judging from your variable references, I'd quess you're coming to PHP from ASP/VBScript. The kinds of errors you're facing are basic formatting errors that could be avoided by reading parts of the PHP line manual, particularly the core language reference:

Want the best answers? Ask the best questions! TANSTAAFL!
 
Hi all thanks for your help so far I just have one more question.

I've changed the code to $name=$_POST["txt_name"] and it is now giving me the following error
Notice: Undefined index: txt_name in C:\Program Files\Apache Group\Apache2\htdocs\guestbook\guestbook1.php on line 9

I know that this means that php can not find a value for "txt_name" but as the form is ment to load up being blank and the user enters their name email comment etc I can't see why it is throwing this error at me.
The only thing I can think of is my friend wrote the origional in php 4 (it works fine) and I'm using php 5

here is the code is brief again for the form and php post method.
Code:
<?php
	require ($_SERVER['DOCUMENT_ROOT']."/guestbook/config/db_config.php");
	
	$connection = mysql_connect($db_host, $db_user, $db_pass) or die("Sorry we can not connect to the database at the moment");

	mysql_select_db ($db_name, $connection);

	
	$name = $_POST["txt_name"];
	$len = strlen ($name);
	if ($len > 0)
	{
	$email = $_POST["txt_email"];
	$comment = $_POST["txt_comment"];
	$date = time ();
	
	$query = "INSERT INTO guestbook (autoid, name, email, comment, date_auto) VALUES (NULL, '$name', '$email', 

'$comment', '$date') ";
	mysql_query ($query, $connection) or die (mysql_error());
	}	
?>		

<html>
<head>
<title>guestbook</title>

</head>

<body>
<table width="70%"  border="0" align="center">
  <tr>
    <td>Welcome to</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
      <table width="100%"  border="0" align="center" bgcolor="#CCCCCC">
        <tr>
          <td width="13%">Name:</td>
          <td width="87%"><input type="text" input name="txt_name" id="txt_name"></td>
        </tr>
        <tr>
          <td>Email:</td>
          <td><input type="text" input name="txt_email" id="txt_email"></td>
        </tr>
        <tr>
          <td>Comments:</td>
          <td><textarea name="txt_comment" cols="50" rows="10" id="txt_comment"></textarea></td>
        </tr>
        <tr>
          <td><input type="submit" name="Submit" value="Submit"></td>
          <td>&nbsp;</td>
        </tr>
      </table>
    </form></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>

Thanks in advance for any help

Graham.
 
It's because your code references the variable before it exists. Until your form has been submitted to the script, there will be no $_POST['txt_name']

If you're going to have the same script output the form and process the submitted data from the form, the core structue of your script must be something like:

Code:
<?php
if (isset ($_POST['txt_name']))
{
  //process the data
}
else
{
  //output the form
}
>?




Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks sleipnir I'll have a look at this and see what I can come up with.

Graham.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top