我有3个文件
1)show_createtable.html
2)do_showfielddef.PHP
3)do_showtble.PHP
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意外$结束”
<? $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,-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>"; }
你在最后一个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 .=","; } }