Jump to content

(question from a studen)t error on line 1?


studentuser55

Recommended Posts

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.