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

发布于 2024-08-29 06:47:51 字数 3995 浏览 3 评论 0原文

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

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) 该脚本验证字段并创建另一个表单来输入所有表行。 这也很好用!

<?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>

问题就在这里 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>

I have 3 files
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!

<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!

<?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"

<?
$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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

抹茶夏天i‖ 2024-09-05 06:47:51
$result = mysql_query($sql, $connection) or die(mysql_error());
if ($result) {
    $msg = "<p>" .$_POST[table_name]." has been created!</p>";
}

您在最后一个 if 语句中缺少 },并且您的 for 循环也缺少 }

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 .=",";
    }
} 
$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

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 .=",";
    }
} 
屋顶上的小猫咪 2024-09-05 06:47:51

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

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

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 for loop or the last if are missing.

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

浮萍、无处依 2024-09-05 06:47:51

您必须关闭 for 表达式块:

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

You must close the for expression block:

for ($i = 0; $i < count($_POST[field_name]); $i++) {
    $sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i];
    // ...
}
゛清羽墨安 2024-09-05 06:47:51

您的 for 循环未终止。您缺少 }


for ($i = 0; $i < count($_POST[field_name]); $i++) {
  $sql .= $_POST[字段名称][$i]." ".$_POST[字段类型][$i];
}

正如其他人所指出的,最后一个 if 语句还缺少 }


if ($结果) {
  $msg = "

" .$_POST[table_name]." 已创建!< /p>";
}

Your for loop is not terminated. You are missing a }


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 if statement:


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

亽野灬性zι浪 2024-09-05 06:47:51

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

short_open_tag = 关闭

您在第 1 行不久就打开了 php 标签
只需查找并替换所有

in your php.ini (php configuration) change :

short_open_tag = Off

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

冷情 2024-09-05 06:47:51

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

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.

被翻牌 2024-09-05 06:47:51

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

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 = On in my php.ini.
This solved this error for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文