[英]PHP syntax error “unexpected $end”
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. 1)第一个文件用于为数据库创建新表,它是一个具有2个输入的表,表名和字段数。 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. 2)此脚本验证字段并创建另一个表单以输入所有表格行。 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. 问题在这里3)这个表单创建表并将它们输入数据库。 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"
我在第37行收到错误“解析错误:语法错误,第37行/home/admin/domains/domaina.com.au/public_html/do_createtable.php意外$结束”
<?
$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>
$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循环也缺少了}
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 .=",";
}
}
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. 在你的情况下,收盘
}
的有些像你的控制结构for
循环或在最后if
缺少。
You should use proper indentation and an editor that highlights bracket pairs to have a visual aid to avoid such errors. 您应该使用适当的缩进和一个突出显示括号对的编辑器,以获得视觉帮助以避免此类错误。
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 你在第1行很快就打开了php标签
just find and replace all <?
只需查找并替换所有<? with
<?php
用<?php
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];
// ...
}
Your for
loop is not terminated. 你的
for
循环没有终止。 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
语句也有一个缺失}
:
if ($result) {
$msg = "< p>" .$_POST[table_name]." has been created!< /p>";
}
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
. 事实证明我需要在我的
php.ini
启用short_open_tag = On
。
This solved this error for me. 这为我解决了这个错误。
Stylistic tip: Use HEREDOC s to assign blocks of text to a variable, instead of the hideous multi-line-with-tons-of-escaping-backslashes constructs you're using. 文体提示:使用HEREDOC将文本块分配给变量,而不是您正在使用的可靠的多行转换 - 反斜杠构造。 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.
如果/当你碰巧忘记了某个地方并且用解析错误破坏了脚本时,它们更容易阅读并且更不容易出错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.