Hi,
I have modified the following php code to create tables fields dynamically. Basically I created a simple html form to enter the table name and pass the table name to the php file to create fields for the table. in the php file the user can enter details of the fields or delete as appropriate, so far the code works fine. the problem I have is to create the actual table. I've tried different ways but does not work (every time I entered new fields keep loosing the table name), can some one help me please to fix this problem please. Here is the code
<?php
session_start();
header("Cache-control: private"); //IE 6 Fix
session_register('myArray');
$myArray = $SESSION['myArray'];
$table_name = trim($POST['table_name']);
$field_name = trim($POST['field_name']);
$field_type = trim($POST['field_type']);
$field_length = trim($POST['field_length']);
$field_default = trim($POST['field_default']);
$field_primary = trim($POST['field_primary']);
$field_notNull = trim($POST['field_notNull']);
$field_unsigned = trim($POST['field_unsigned']);
$field_autoinc = trim($POST['field_autoinc']);
$rb = trim($POST['radiobutton']); // Delete Key
$create = trim($POST['create']);
class Table
{
// CLASS DATA IS SPECIFIED HERE
var $field_name;
var $field_type;
var $field_length;
var $field_default;
var $field_primary;
var $field_notNull;
var $field_unsigned;
var $field_autoinc;
// OUTPUT FROM THE CLASS
function displayData()
{
return "<td>$this->field_name</td><td>$this->field_type</td><td>$this->field_length</td>
<td>$this->field_default</td><td>$this->field_primary</td><td>$this->field_notNull</td>
<td>$this->field_unsigned</td><td>$this->field_autoinc</td>";
}
// INPUT - SET THE TABLE NAME
function setTableName($newTableName)
{
$this->table_name = trim($newTableName);
}
// INPUT - SET THE FIELD NAME
function setFieldName($newFieldName)
{
$this->field_name = trim($newFieldName);
}
// INPUT - SET THE FIELD TYPE
function setFieldType($newFieldType)
{
$this->field_type = trim($newFieldType);
}
// INPUT - SET THE FIELD LENGTH
function setFieldLength($newFieldLength)
{
$this->field_length = trim($newFieldLength);
}
function setFieldDefault($newFieldDefault)
{
$this->field_default = trim($newFieldDefault);
}
function setFieldPrimary($newFieldPrimary)
{
$this->field_primary = trim($newFieldPrimary);
}
function setFieldnotNull($newFieldnotNull)
{
$this->field_notNull = trim($newFieldnotNull);
}
function setFieldUnsigned($newFieldUnsigned)
{
$this->field_unsigned = trim($newFieldUnsigned);
}
function setFieldAutoinc($newFieldAutoinc)
{
$this->field_autoinc = trim($newFieldAutoinc);
}
}
?>
<html>
<head>
<title>Add and Delete Fields</title>
</head>
<body>
<p><a href="newtable.php">Back</a></p>
<h1>Add and Delete Fields for Table <? echo "$table_name";?></h1>
<form name="form1" method="post" action="newtable.php">
<table border="1" cellspacing="0" cellpadding="4">
<tr>
<td><div align="left"><b><i>Table Name:</i></b></div></td>
<td> <input type="text" name="table_name"> </td>
</tr>
<tr>
<td><div align="left"><b>Field Name:</b></div></td>
<td> <input type="text" name="field_name"> </td>
</tr>
<tr>
<td><div align="left"><b>Type:</b></div></td>
<td align=center>
<select name="field_type">
<option value="bigint">bigint</option>
<option value="blob">blob</option>
<option value="char">char</option>
<option value="date">date</option>
<option value="decimal">decimal</option>
<option value="double">double</option>
<option value="enum">enum</option>
<option value="float">float</option>
<option value="int">int</option>
<option value="longblob">longblob</option>
<option value="longtext">longtext</option>
<option value="timestamp">timestamp</option>
<option value="tinyblob">tinyblob</option>
<option value="tinytext">tinytext</option>
<option value="varchar">varchar</option>
<option value="year">year</option>
</select>
</td>
</tr>
<tr>
<td><div align="left"><b>Length:</b></div></td>
<td> <input type="text" name="field_length"> </td>
</tr>
<tr>
<td><div align="left"><b>Default:</b></div></td>
<td> <input type="text" name="field_default"> </td>
</tr>
<tr>
<td><div align="left"><b>Primary Key ?:</b></div></td>
<td><input type="checkbox" name="field_primary" value="PRIMARY KEY"></td>
</tr>
<tr>
<td><div align="left"><b>Not Null ?:</b></div></td>
<td><input type="checkbox" name="field_notNull" value="NOT NULL"></td>
</tr>
<tr>
<td><div align="left"><b>Unsigned ?:</b></div></td>
<td><input type="checkbox" name="field_unsigned" value="UNSIGNED"></td>
</tr>
<tr>
<td><div align="left"><b>Auto Increment ?:</b></div></td>
<td><input type="checkbox" name="field_autoinc" value="AUTO INCREMENT"></td>
</tr>
<tr>
<td colspan="2"><div align="right">
<input type="submit" name="Submit" value="Submit">
</div></td>
</tr>
</table>
</form>
<?php
// THIS LINE IS FOR DEBUGGING ONLY
echo"Table Name = $table_name<br>";
echo"Field Name = $field_name<br>";
echo"Type = $field_type<br>";
echo"Length = $field_length<br>";
echo"Default = $field_default<br>";
echo"Primary Key ? = $field_primary<br>";
echo"Not Null ? = $field_notNull<br>";
echo"Unsigned ? = $field_unsigned<br>";
echo"Auto Increment ? = $field_autoinc<br>";
if ($field_name == "")
{
echo "<p>Blank Table or Field Name - List not updated.</p>\n";
}
else
{
// CREATE A NEW Table OBJECT AND
$table = new Table();
$table->setTableName($table_name);
$table->setFieldName($field_name);
$table->setFieldType($field_type);
$table->setFieldLength($field_length);
$table->setFieldDefault($field_default);
$table->setFieldPrimary($field_primary);
$table->setFieldnotNull($field_notNull);
$table->setFieldUnsigned($field_unsigned);
$table->setFieldAutoinc($field_autoinc);
echo "<p>Added a New Field to the List.</p>\n";
// ADD THE NEW OBJECT TO THE NEXT FREE ARRAY LOCATION
$myArray[] = $table;
$_SESSION['myArray'] = $myArray;
}
$count = count($myArray); // The number of items in the array
echo "<p>Inserted Field = $count</p>\n";
echo "<p>Delete Key = $rb</p>\n";
// DELETE AN ARRAY ITEM IF A RADIOBUTTON WAS CHECKED
if ($create)
{
echo "CREATE TABLE $table_name (";
foreach($myArray as $key => $value)
{
// Display a table row
echo "<br>";
echo "$value->field_name ";
echo "$value->field_type";
echo "(";
echo "$value->field_length";
echo ")";
echo " ";
echo "$value->field_default";
echo " ";
echo "$value->field_primary";
echo " ";
echo "$value->field_notnull";
echo " ";
echo "$value->field_unsigned";
echo " ";
echo "$value->field_autoinc";
echo ",";
echo "\n";
}
echo "<br>";
echo ");";
//echo "<h3>Query Created is</h3>";
//echo "<hr>";
//echo "CREATE table $myArray[0] VALUES";
// header("Location: mainadminmenu.php");
exit;
}
if ($rb <> "")
{
unset($myArray[$rb]); // This removes the item indexed by $rb
$_SESSION['myArray'] = $myArray; // Save new array into session variable.
}
// Display the data in a form with radio buttons used to select an item to be deleted.
echo "<form name=\"form2\" method=\"post\" action=\"newtable.php\">\n";
echo "<table border=\"1\" cellpadding=\"4\" border=\"1\" cellspacing=\"0\">\n";
echo "<tr><td><b>Delete</b></td>\n";
echo "<td><b>Record #</b></td>\n";
echo "<td><b>Field Name</b></td>\n";
echo "<td><b>Type</b></td>\n";
echo "<td><b>Length</b></td>\n";
echo "<td><b>Default</b></td>\n";
echo "<td><b>Primary Key ?</b></td>\n";
echo "<td><b>Not Null ?</b></td>\n";
echo "<td><b>Unsigned ?</b></td>\n";
echo "<td><b>Auto Increment ?</b></td></tr>\n";
// Test to ensure the array exists. If it does, display it.
if (isset($myArray))
{
foreach($myArray as $key => $value)
{
// Display a table row
echo "<tr><td><input name=\"radiobutton\" type=\"radio\" value=\"$key\"></td>\n";
echo "<td align=\"center\">$key</td>".$value->displayData()."</tr>\n";
}
}
// Display the delete and create table button
echo "<tr><td colspan=\"10\" align=\"right\">\n";
echo "<input type=\"submit\" name=\"Delete\" value=\"Delete\">\n";
echo "<input type=\"submit\" name=\"create\" value=\"Create Table!\">\n";
echo "</td></tr>\n";
echo "</table>\n";
echo "</form>\n";
?>
</body>
</html>
Please help me
Thank you.