PHP 语法错误“意外的 $end”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2661620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:15:07  来源:igfitidea点击:

PHP syntax error “unexpected $end”

phpsyntaxsyntax-error

提问by Hymansta

I have 3 files 1) show_createtable.html 2) do_showfielddef.php 3) do_showtble.php

我有 3 个文件 1) show_createtable.html 2) do_showfielddef.php 3) do_showtble.php

1) First file is for creating a new table for a data base, it is a fom with 2 inputs, Table Name and Number of Fields. THIS WORKS FINE!

1) 第一个文件用于为数据库创建一个新表,它是一个具有 2 个输入、表名和字段数的表单。这很好用!

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</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="30" /></p>
<p><input type="submit" name="submit" value="go to step2" /></p>
</form>


</body>
</html>

2) this script validates fields and createa another form to enter all the table rows. This for also WORKS FINE!

2)此脚本验证字段并创建另一个表单以输入所有表格行。这也很好用!

<?php
//validate important input
if ((!$_POST[table_name]) || (!$_POST[num_fields])) {
    header( "location: show_createtable.html");
           exit;
}

//begin creating form for display
$form_block = "
<form action=\"do_createtable.php\" method=\"post\">
<input name=\"table_name\" type=\"hidden\" value=\"$_POST[table_name]\">
<table cellspacing=\"5\" cellpadding=\"5\">
  <tr>
    <th>Field Name</th><th>Field Type</th><th>Table Length</th>
  </tr>";

//count from 0 until you reach the number fo fields
for ($i = 0; $i <$_POST[num_fields]; $i++) {
  $form_block .="
  <tr>
  <td align=center><input type=\"texr\" 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>";
}

//finish up the form 
$form_block .= "
<tr>
    <td align=center colspan=3><input type =\"submit\" value=\"create table\">
    </td>
</tr>
</table>
</form>";

?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create a database table: Step 2</title>
</head>

<body>
<h1>defnie fields for <? echo "$_POST[table_name]"; ?> 
</h1>
<? echo "$form_block"; ?>

</body>
</html>

Problem is here 3) this form creates the tables and enteres them into the database. I am getting an error on line 37 "Parse error: syntax error, unexpected $end in /home/admin/domains/domaina.com.au/public_html/do_createtable.php on line 37"

问题在这里 3) 这个表格创建表格并将它们输入到数据库中。我在第 37 行收到错误“解析错误:语法错误,第 37 行在 /home/admin/domains/domaina.com.au/public_html/do_createtable.php 中意外 $end”

<?
$db_name = "testDB";

$connection = @mysql_connect("localhost", "admin_user", "pass")
    or die(mysql_error());

$db = @mysql_select_db($db_name, $connection)
    or die(mysql_error());

$sql = "CREATE TABLE $_POST[table_name](";
    for ($i = 0; $i < count($_POST[field_name]); $i++) {
        $sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i];
    if ($_POST[field_length][$i] !="") {
        $sql .=" (".$_POST[field_length][$i]."),";
        } else {
            $sql .=",";
        }
$sql = substr($sql, 0, -1);
$sql .= ")";

$result = mysql_query($sql, $connection) or die(mysql_error());
if ($result) {
    $msg = "<p>" .$_POST[table_name]." has been created!</p>";

?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create A Database Table: Step 3</title>
</head>

<body>
<h1>Adding table to <? echo "$db_name"; ?>...</h1>
<? echo "$msg"; ?>
</body>
</html>

回答by x4tje

$result = mysql_query($sql, $connection) or die(mysql_error());
if ($result) {
    $msg = "<p>" .$_POST[table_name]." has been created!</p>";
}

you missing a }in your last if statement, and your for loop is missing a }too

你错过了}在过去的if语句,和你的循环缺少}

for ($i = 0; $i < count($_POST[field_name]); $i++) {
    $sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i];
    if ($_POST[field_length][$i] !="") {
      $sql .=" (".$_POST[field_length][$i]."),";
    } else {
        $sql .=",";
    }
} 

回答by Gumbo

This error message means that a control structure block isn't closed properly. In your case the closing }of some of your control structures like the forloop or the last ifare missing.

此错误消息意味着控制结构块未正确关闭。在您的情况下,缺少}某些控制结构(例如for循环或最后一个)的关闭if

You should use proper indentation and an editor that highlights bracket pairs to have a visual aid to avoid such errors.

您应该使用适当的缩进和突出显示括号对的编辑器,以获得避免此类错误的视觉帮助。

回答by yassin

You must close the for expression block:

您必须关闭 for 表达式块:

for ($i = 0; $i < count($_POST[field_name]); $i++) {
    $sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i];
    // ...
}

回答by user3095614

? in your php.ini(php configuration) change :

? 在您的php.ini(php 配置)更改中:

short_open_tag = Off

short_open_tag = 关闭


you opened php tag shortly at line 1
just find and replace all <?with <?php


您在第 1 行很快打开了 php 标签,
只需找到并替换所有<? <?php

回答by codaddict

Your forloop is not terminated. You are missing a }

你的for循环没有终止。你缺少一个}

for ($i = 0; $i < count($_POST[field_name]); $i++) {
  $sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i];
}

for ($i = 0; $i < count($_POST[field_name]); $i++) {
  $sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i];
}

And as pointed by others there is also a missing }for the last ifstatement:

正如其他人所指出的},最后一条if语句也有缺失:

if ($result) {
  $msg = "< p>" .$_POST[table_name]." has been created!< /p>";
}

if ($result) {
  $msg = "< p>" .$_POST[table_name]." has been created!< /p>";
}

回答by Marc B

Stylistic tip: Use HEREDOCs to assign blocks of text to a variable, instead of the hideous multi-line-with-tons-of-escaping-backslashes constructs you're using. They're far easier to read and less error prone if/when you happen to forget a \ somewhere and break the script with a parse error.

风格提示:使用HEREDOC将文本块分配给变量,而不是您正在使用的可怕的多行转义反斜杠构造。如果/当您碰巧在某处忘记了 \ 并使用解析错误破坏脚本时,它们更容易阅读且不易出错。

回答by Anil

I just solved this error, after checking my code, I had no open tags/braces.
For me, I got this error when moving to a amazon server.
It turns out I needed to enable short_open_tag = Onin my php.ini.
This solved this error for me.

我刚刚解决了这个错误,在检查我的代码后,我没有打开的标签/大括号。
对我来说,我在移动到亚马逊服务器时遇到了这个错误。
事实证明我需要short_open_tag = On在我的php.ini.
这为我解决了这个错误。