PHP语法错误“意外$end”

前端之家收集整理的这篇文章主要介绍了PHP语法错误“意外$end”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有3个文件 @H_502_1@1)show_createtable.html @H_502_1@2)do_showfielddef.PHP @H_502_1@3)do_showtble.PHP

1)第一个文件用于为数据库创建新表,它是一个具有2个输入的表,表名和字段数.这个工作很精细!

  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head>
  3. <Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Untitled Document</title>
  5. </head>
  6.  
  7. <body>
  8. <h1>Step 1: Name and Number</h1>
  9. <form method="post" action="do_showfielddef.PHP" />
  10. <p><strong>Table Name:</strong><br />
  11. <input type="text" name="table_name" size="30" /></p>
  12. <p><strong>Number of fields:</strong><br />
  13. <input type="text" name="num_fields" size="30" /></p>
  14. <p><input type="submit" name="submit" value="go to step2" /></p>
  15. </form>
  16.  
  17.  
  18. </body>
  19. </html>

2)此脚本验证字段并创建另一个表单以输入所有表格行.@H_502_1@这也很精致!

  1. <?PHP
  2. //validate important input
  3. if ((!$_POST[table_name]) || (!$_POST[num_fields])) {
  4. header( "location: show_createtable.html");
  5. exit;
  6. }
  7.  
  8. //begin creating form for display
  9. $form_block = "
  10. <form action=\"do_createtable.PHP\" method=\"post\">
  11. <input name=\"table_name\" type=\"hidden\" value=\"$_POST[table_name]\">
  12. <table cellspacing=\"5\" cellpadding=\"5\">
  13. <tr>
  14. <th>Field Name</th><th>Field Type</th><th>Table Length</th>
  15. </tr>";
  16.  
  17. //count from 0 until you reach the number fo fields
  18. for ($i = 0; $i <$_POST[num_fields]; $i++) {
  19. $form_block .="
  20. <tr>
  21. <td align=center><input type=\"texr\" name=\"field name[]\"
  22. size=\"30\"></td>
  23. <td align=center>
  24. <select name=\"field_type[]\">
  25. <option value=\"char\">char</option>
  26. <option value=\"date\">date</option>
  27. <option value=\"float\">float</option>
  28. <option value=\"int\">int</option>
  29. <option value=\"text\">text</option>
  30. <option value=\"varchar\">varchar</option>
  31. </select>
  32. </td>
  33. <td align=center><input type=\"text\" name=\"field_length[]\" size=\"5\">
  34. </td>
  35. </tr>";
  36. }
  37.  
  38. //finish up the form
  39. $form_block .= "
  40. <tr>
  41. <td align=center colspan=3><input type =\"submit\" value=\"create table\">
  42. </td>
  43. </tr>
  44. </table>
  45. </form>";
  46.  
  47. ?>
  48.  
  49. <html>
  50. <head>
  51. <Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  52. <title>Create a database table: Step 2</title>
  53. </head>
  54.  
  55. <body>
  56. <h1>defnie fields for <? echo "$_POST[table_name]"; ?>
  57. </h1>
  58. <? echo "$form_block"; ?>
  59.  
  60. </body>
  61. </html>

问题在这里@H_502_1@3)此表单创建表并将它们输入数据库.@H_502_1@我在第37行收到错误“解析错误:语法错误,第37行/home/admin/domains/domaina.com.au/public_html/do_createtable.PHP意外$结束”

  1. <?
  2. $db_name = "testDB";
  3.  
  4. $connection = @MysqL_connect("localhost","admin_user","pass")
  5. or die(MysqL_error());
  6.  
  7. $db = @MysqL_select_db($db_name,$connection)
  8. or die(MysqL_error());
  9.  
  10. $sql = "CREATE TABLE $_POST[table_name](";
  11. for ($i = 0; $i < count($_POST[field_name]); $i++) {
  12. $sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i];
  13. if ($_POST[field_length][$i] !="") {
  14. $sql .=" (".$_POST[field_length][$i]."),";
  15. } else {
  16. $sql .=",";
  17. }
  18. $sql = substr($sql,-1);
  19. $sql .= ")";
  20.  
  21. $result = MysqL_query($sql,$connection) or die(MysqL_error());
  22. if ($result) {
  23. $msg = "<p>" .$_POST[table_name]." has been created!</p>";
  24.  
  25. ?>
  26.  
  27. <html>
  28. <head>
  29. <Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  30. <title>Create A Database Table: Step 3</title>
  31. </head>
  32.  
  33. <body>
  34. <h1>Adding table to <? echo "$db_name"; ?>...</h1>
  35. <? echo "$msg"; ?>
  36. </body>
  37. </html>
  1. $result = MysqL_query($sql,$connection) or die(MysqL_error());
  2. if ($result) {
  3. $msg = "<p>" .$_POST[table_name]." has been created!</p>";
  4. }

你在最后一个if语句中错过了一个},而你的for循环也缺少了}

  1. for ($i = 0; $i < count($_POST[field_name]); $i++) {
  2. $sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i];
  3. if ($_POST[field_length][$i] !="") {
  4. $sql .=" (".$_POST[field_length][$i]."),";
  5. } else {
  6. $sql .=",";
  7. }
  8. }

猜你在找的PHP相关文章