解析错误:语法错误,意外'<<' (T_SL)

时间:2014-01-05 08:09:43

标签: php mysql

我有这段代码,但我得到了Parse error: syntax error, unexpected '<<' (T_SL)

<?php

$id = $_GET['id'];
$con = mysqli_connect("localhost","root","root","chess");
$result = mysqli_query($con , "select * from members where id=$id");

echo <<<_END;
<form action = "updatemem.php" method = 'POST'>
<br><br><br><br>

<center>

<b>EDIT RECORD</b><br><br>

while($row = mysqli_fetch_array($result))
{
echo "ID: <input type='text' name='id' value='$row[id]'><br/>";
echo "Lastname: <input type='text' name='lastname' value='$row[lastname]'><br/>";
echo "Codename: <input type='password' name='codename' value='$row[codename]'><br/>";
echo "Location: <input type='text' name='location' value='$row[location]'><br/>";
echo "Rank: <input type='text' name='rank' value='$row[rank]'><br/>";
}


<br>
<input type="submit" value="Edit Record">
</center>
</form>
?>

错误在第7行,即echo <<<_END;。我已经尝试echo<<END;并检查空格,因为其他用户在其他线程上发出了相同的错误。修复此错误的可能解决方案是什么?

4 个答案:

答案 0 :(得分:3)

您的错误是:

  

解析错误:语法错误,意外'&lt;&lt;&lt; (T_SL)

查看错误。它说这是一个解析&amp;具有<<的代码周围的语法错误。因此,查看代码并找到<<项。那么究竟是什么echo <<<_END;?有人说这是一个HEREDOC问题,但似乎并不那么简单。我建议改为使用这个清理过的代码:

$id = $_GET['id'];
$con = mysqli_connect("localhost","root","root","chess");
$result = mysqli_query($con , "select * from members where id=$id");

echo <<<EOT
<form action = "updatemem.php" method = 'POST'>
<br><br><br><br>
<center>
<b>EDIT RECORD</b><br><br>
EOT;

while($row = mysqli_fetch_array($result)) {
  echo "ID: <input type='text' name='id' value='$row[id]'><br/>";
  echo "Lastname: <input type='text' name='lastname' value='$row[lastname]'><br/>";
  echo "Codename: <input type='password' name='codename' value='$row[codename]'><br/>";
  echo "Location: <input type='text' name='location' value='$row[location]'><br/>";
  echo "Rank: <input type='text' name='rank' value='$row[rank]'><br/>";
}

echo <<<EOT
<br>
<input type="submit" value="Edit Record">
</center>
</form>
EOT;

问题是你有一个奇怪的HEREDOC

echo <<<_END;

这没有任何意义。错误是PHP基本上就是这一行,而不是理解<<<_END;的含义和死亡。

然后最重要的是你有整个while声明:

while($row = mysqli_fetch_array($result)) {

通过尝试HEREDOC,在未关闭的HTML之后放置PHP代码。接下来是更多HTML:

<br>
<input type="submit" value="Edit Record">
</center>
</form>

那些东西没有echo个陈述。它就像HTML一样漂浮在那里。

这一切都意味着你并不真正理解HTML&amp; PHP混合在一起。这是公平的,但你需要了解这一点,以避免这样的简单问题。

根据我的口味,这就是我如何格式化这段代码。我只是简单地将它全部用于PHP并使用连接来连接所有的echo项目&amp;将它们整齐地放在不同的线上,以便于阅读。

$id = $_GET['id'];
$con = mysqli_connect("localhost","root","root","chess");
$result = mysqli_query($con , "select * from members where id=$id");

echo '<form action = "updatemem.php" method="POST">'
   . '<br><br><br><br>'
   . '<center>'
   . '<b>EDIT RECORD</b><br><br>'
   ;

while($row = mysqli_fetch_array($result)) {
  echo "ID: <input type='text' name='id' value='$row[id]'><br/>"
     . "Lastname: <input type='text' name='lastname' value='$row[lastname]'><br/>"
     . "Codename: <input type='password' name='codename' value='$row[codename]'><br/>"
     . "Location: <input type='text' name='location' value='$row[location]'><br/>"
     . "Rank: <input type='text' name='rank' value='$row[rank]'><br/>"
     ;
}

echo '<br>'
   . '<input type="submit" value="Edit Record">'
   . '</center>'
   . '</form>'
   ;

答案 1 :(得分:1)

似乎您的 HEREDOC 缺少最终限制。

这是一个没有 HEREDOC 解决方案

<?php

$id = $_GET['id'];
$con = mysqli_connect("localhost","root","root","chess");
$result = mysqli_query($con , "select * from members where id=$id");

?>

<form action = "updatemem.php" method = 'POST'>
<br><br><br><br>

<center>

<b>EDIT RECORD</b><br><br>
<?php

while($row = mysqli_fetch_array($result))
{
    echo "ID: <input type='text' name='id' value='$row[id]'><br/>";
    echo "Lastname: <input type='text' name='lastname' value='$row[lastname]'><br/>";
    echo "Codename: <input type='password' name='codename' value='$row[codename]'><br/>";
    echo "Location: <input type='text' name='location' value='$row[location]'><br/>";
    echo "Rank: <input type='text' name='rank' value='$row[rank]'><br/>";
}
?>

<br>
<input type="submit" value="Edit Record">
</center>
</form>

答案 2 :(得分:0)

您错过了END来结束heredoc

你似乎也在滥用 heredoc;你不能在里面混合PHP和HTML。您的while循环不起作用,它将作为字符串输出到浏览器。无论如何,这对heredoc来说真的不合适;你真正想要的是简单地用?>切换出PHP上下文:

<?php

$id = $_GET['id'];
$con = mysqli_connect("localhost","root","root","chess");
$result = mysqli_query($con , "select * from members where id=$id");

?>
<form action = "updatemem.php" method = 'POST'>
<br><br><br><br>

<center>

<b>EDIT RECORD</b><br><br>

<?php

while($row = mysqli_fetch_array($result))
{
echo "ID: <input type='text' name='id' value='$row[id]'><br/>";
echo "Lastname: <input type='text' name='lastname' value='$row[lastname]'><br/>";
echo "Codename: <input type='password' name='codename' value='$row[codename]'><br/>";
echo "Location: <input type='text' name='location' value='$row[location]'><br/>";
echo "Rank: <input type='text' name='rank' value='$row[rank]'><br/>";
}

?>
<br>
<input type="submit" value="Edit Record">
</center>
</form>

答案 3 :(得分:0)

检查手册中的heredoc语法:http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

不要在该行的末尾加上分号。尝试

echo <<<_END

此外,您需要像

一样结束块
_END;