hi, i am constantly adding the same tables (names and values) to different databases. I came up with a code that will ask for the database name and then auto create the tables(these tables and there values never change)
This code doesnt seem to want to work, this is what i have:
<<<<<<<<<<<form>>>>>>>>>>>
<?
$form_block = "
<FORM METHOD=\"POST\" ACTION=\"creatdbtbl2.php\">
";
// create letters2 table
// 2 fields letter_id letter_name
$form_block .= "
<INPUT TYPE=\"hidden\" NAME=\"field_name[1]\" value=\"letter_id\"</TD>
<INPUT TYPE=\"hidden\" NAME=\"field_type[1]\" value=\"int\"</TD>
<INPUT TYPE=\"hidden\" NAME=\"field_length[1]\" value=\"\"></TD>
<INPUT TYPE=\"hidden\" NAME=\"primary[1]\" VALUE=\"Y\"></TD>
<INPUT TYPE=\"hidden\" NAME=\"auto_increment[1]\" VALUE=\"Y\"></TD>
<INPUT TYPE=\"hidden\" NAME=\"field_name[2]\" value=\"letter_name\"</TD>
<INPUT TYPE=\"hidden\" NAME=\"field_type[2]\" value=\"varchar\"</TD>
<INPUT TYPE=\"hidden\" NAME=\"field_length[2]\" value=50></TD>
<INPUT TYPE=\"hidden\" NAME=\"primary[2]\" VALUE=\"n\"></TD>
<INPUT TYPE=\"hidden\" NAME=\"auto_increment[2]\" VALUE=\"n\"></TD>
";
$form_block .= "
<strong>Enter the database where information should be placed:</strong><INPUT TYPE=\"text\" name=\"db_name\">
<TD ALIGN=CENTER COLSPAN=3><INPUT TYPE=\"submit\" VALUE=\"Create Table\"></TD>
</TABLE>
</FORM>
";
?>
<HEAD><TITLE>Create a Database Table: Step 2</TITLE>
</HEAD>
<BODY>
<H1>Define fields for <? echo "$table_name"; ?></H1>
<? echo "$form_block"; ?>
</BODY>
------To do the processing my sql part of the code is:
$sql = "CREATE TABLE letters2 (";
for ($i = 0; $i < 3; $i++) {
$sql .= "$field_name[$i] $field_type[$i]";
if ($auto_increment[$i] == "Y") {
$additional = "NOT NULL auto_increment";
} else {
$additional = "";
}
if ($primary[$i] == "Y") {
$additional .= ", primary key ($field_name[$i])";
} else {
$additional .= "";
}
if ($field_length[$i] != "") {
$sql .= " ($field_length[$i]) $additional ,";
} else {
$sql .= " $additional ,";
}
}
$sql = substr($sql, 0, -1);
$sql .= ")";
// execute the query
$result = mysql_query($sql,$connection) or die(mysql_error());
if ($result) {
$msg = "<P>letters2 has been created!</p>
";
}
------- Here is the error
You have an error in your SQL syntax near 'letter_id int ,letter_name varchar (50) )' at line 1
Sorry this is so long, but if anyone can help i really appreciate it.
Thanks