studentuser55 Posted November 14, 2011 Share Posted November 14, 2011 hello, I have multiple scripts I am writing for class and the very last script does not seem to be working and most of us are stumped . the first two seem to work fine but I am going to post them too just in case there is something on the previous scripts I am missing that is causing the error. the scripts are suppose to make a table then the final one lists the tables I believe. 1st script Do-Creaable.html ----------------------------------------------------------------------------------------------------------- <html> <title> input page</title> <head><strong> Name and Number of fields</strong></head> <body> <form method ="post" action="do_showfileddef.php"> Table Name: <br> <input type="text" name="table_name"><p> <br> Number of fields:<p> <input type="text" name="num_fields"><p> <input type="submit" value="go to step two"> </form> </body> </html> --------------------------------------------------------------------------------------------------------------------- second script do show_fieldeff.php --------------------------------------------------------------- <? if ((!$_POST['table_name']) || (!$_POST['num_fields'])) { header ("location: show_creatable.html"); exit; } $form_block =" <FORM METHOD=\"POST\"ACTION=\"do_creatable.php\"> <iINPUT TYPE=\"hidden\"NAME=\"table_name\" VALUE\"$_POST[table_name]\"> <TABLE CELLSPACING=5 CELLPADDING=5> <TR> <TH>field name</TH><TH>filed type</TH> <TH>field length</TH></TR>"; for($i =0; $i < $_POST['num_fields']; $i++) { $form_block .=" <tr> <td allign=center><input type=\"text\"name=\"filed_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\">textchar</OPTION> <OPTION VALUE=\"varchar\">varchar</OPTION> </SELECT> </td> <TD ALIGN=CENTER><INPUT TYPE=\"text\"NAME=\"field_length[]\" SIZE=\"5\"></td> </tr>"; } $form_block .=" <tr> <td allign=center colspan=3><input type=\"submit\"value=\"create table\"></td> </tr> </table> </form>"; ?> <html> <head> <title> create a Database table:step 2</title> </head> <body> <h1> Define fields for <? echo "$_POST[table_name]"; ?> </h1> <?echo "$form_block";?> </body> </html> ----------------------------------------------------------------------------------------------------------------- third script do_creatable.php _____________________________________________________________________________________ $db_name = "testdb"; $connection = @mysql_connect("localhost","root","") 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_length"][$i] ." ".$_POST['field_type'][$i]; if ($_POST ["field_length"] [$i] !="") { $sql .= "(".$_POST ["field_length"] [$i]."),"; } else { $sql .= ","; } } $sql = substr($sql,0,-1); $sql .=")"; $results = mysql_query ($sql,$connection) or die (mysql_error()); if ($results) { $msg="<P>".$_POST["table_name"]."has been created!</P>"; } ------------------------------------------------------------------------------------------------------------------ some where on that third script is an error but it keeps telling me bad syntax on line one. most of the class is having errors with this script and no one can find the cause its been like 2 weeks now. can someone help me? please bare in mind I am a student and some functions of php I am unfammiliar with Quote Link to comment https://forums.phpfreaks.com/topic/251097-question-from-a-student-error-on-line-1/ Share on other sites More sharing options...
joel24 Posted November 14, 2011 Share Posted November 14, 2011 can you copy/paste the error? Quote Link to comment https://forums.phpfreaks.com/topic/251097-question-from-a-student-error-on-line-1/#findComment-1287927 Share on other sites More sharing options...
btellez Posted November 14, 2011 Share Posted November 14, 2011 the following line: $sql = @"CREATE TABLE $_POST[table_name]("; should read: $sql = @"CREATE TABLE $_POST['table_name']("; or if you would like: $sql = @"CREATE TABLE". $_POST["table_name"] ."("; Calling array elements by their name without using quotations around yields a notice.... Quote Link to comment https://forums.phpfreaks.com/topic/251097-question-from-a-student-error-on-line-1/#findComment-1287993 Share on other sites More sharing options...
Pikachu2000 Posted November 14, 2011 Share Posted November 14, 2011 That isn't true when the array element is within a quoted string. Although the more widely accepted way to do it would be to use complex notation rather than omitting the quotes. $sql = @"CREATE TABLE {$_POST['table_name']} ("; Quote Link to comment https://forums.phpfreaks.com/topic/251097-question-from-a-student-error-on-line-1/#findComment-1287994 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.