24

I have been staring at this code for hours now and I cannot figure out where my mistake is. I know this syntax error usually comes up because of some missing or out of place curly brace or some issue with single/double quotes and I'm not sure there is one anywhere in my code. I am only trying to fix my syntax right now so I can get the code to completely compile. Any help would be much appreciated. Here is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<title>Add to and Read from the Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head> 
<body>

<?php
function print_form() {
echo <<<END
         <form action="$_SERVER[PHP_SELF]" method="post">

    <h3>Please put your comments below.</h3>

<input type="hidden" name="stage" value="process" >
<p>Name:</p>
<input type="text" size="30" name="WholeName" />
<p>Comment:</p>
<input type="text" size="200" name="Comment" />

<input type ="submit"  value ="Submit" >
</form>
END;    

    }
function process_form() {

print "<p>adding comment...</p>";

$Name = $_POST['WholeName'];
$Comment = $_POST['Comment'];

    if( preg_match("^[a-zA-Z]+$", $Name)) {
        if( preg_match("^[a-zA-Z0-9]_\-\'[.?!]+$", $Comment)) {
            $sql = "insert into comments1 values ( 
           '$Name', 
           '$Comment')";
           $result = mysql_query($sql) or die("Mysql query failed");
      } else {
        print "invalid name";
        }
  } else {
print "invalid characters";
 }

}

$db = mysql_connect("", "", "");
if (!$db) {
   print "Error - Could not connect to mysql";
   exit;
}  

$er = mysql_select_db("");
if (!$er) {
  print "Error - Could not connect to comments1 database";
  exit;
}

if (isset($_POST['stage']) && ('process' == $_POST['stage'])) {
   process_form();
} else {
    print_form();
}

?>

</body>
</html>
7
  • 14
    It's good practice to state where exactly the error is being reported (i.e., Line number and column number).
    – Gian
    Commented Dec 6, 2011 at 12:44
  • this is indeed "too localized" question Commented Dec 6, 2011 at 12:48
  • Just a note to save yourself a lot of hair-pulling: These kind of errors can easily be tracked down by using a good IDE that inspects your code.
    – Oldskool
    Commented Dec 6, 2011 at 12:52
  • FWIW, the line in question is $Name = $_POST['WholeName']; codepad.viper-7.com/NEJqgK
    – deceze
    Commented Dec 6, 2011 at 12:53
  • It's telling me that the error is occurring on line 32. Can you recommend a good IDE for me to use? Commented Dec 6, 2011 at 12:54

4 Answers 4

50

I stumbled on this question as I had the same error. Mine was due to a slightly different problem and since I resolved it on my own I thought it useful to share here. Original code with issue:

$comment = "$_POST['comment']";

Because of the enclosing double-quotes, the index is not dereferenced properly leading to the assignment error. In my case I chose to fix it like this:

$comment = "$_POST[comment]";

but dropping either pair of quotes works; it's a matter of style I suppose :)

1
  • 1
    In my case it was a stray ` backtick. Geany's syntax highlighter didn't notice it. But Joe's syntax highlighter caught it. Initially your answer helped but I was really fighting an imaginary enemy all along ;-)
    – Jay Brunet
    Commented Jul 16, 2013 at 7:49
42

Your problem is that you're not closing your HEREDOC correctly. The line containing END; must not contain any whitespace afterwards.

1
  • 1
    And it must not contain any whitespaces BEFORE the END; either, as I just found out when trying to format my code for readability.
    – limeygent
    Commented Sep 16, 2015 at 17:08
4

You have extra spaces after END; that cause the heredoc not terminated.

0

Might be a pasting problem, but as far as I can see from your code, you're missing the single quotes around the HTML part you're echo-ing.

If not, could you post the code correctly and tell us what line is causing the error?

Not the answer you're looking for? Browse other questions tagged or ask your own question.