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, can some one help me please. (if i click the create button shall i pass all variables to another php file or i can create on the same page).
Code: Select all
<?php
<?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ї's']);
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 FIRST NAME
function setFieldName($newFieldName)
{
$this->field_name = trim($newFieldName);
}
// INPUT - SET THE LAST NAME
function setFieldType($newFieldType)
{
$this->field_type = trim($newFieldType);
}
// INPUT - SET THE DOB
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> <input type="hidden" name="table_name"> </td>
</tr>
<tr>
<td><div align="left"><b>Field&nbsp;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="mediumblob">mediumblob</option>
<option value="mediumint">mediumint</option>
<option value="mediumtext">mediumtext</option>
<option value="numeric">numeric</option>
<option value="real">real</option>
<option value="set">set</option>
<option value="smallint">smallint</option>
<option value="text">text</option>
<option value="time">time</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"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->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>Record count = $count</p>\n";
echo "<p>Delete Key = $rb</p>\n";
// DELETE AN ARRAY ITEM IF A RADIOBUTTON WAS CHECKED
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 iterates through every array element
// on each iteration, $key is set to the array key
// on each iteration, $value is set to the item stored at the key position
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 button
echo "<tr><td colspan="10" align="right">\n";
echo "<input type="submit" name="Delete" value="Delete">\n";
echo "<input type="submit" name="s" value="Create Table!">\n";
echo "</td></tr>\n";
echo "</table>\n";
echo "</form>\n";
?>
</body>
</html>
?><html>
<head>
<title>Create Table</title>
</head>
<body>
<h1><center>You can create Table using this form</center></h1>
<br>
<hr>
<p>
<h3><center>Please enter the Table name</center></h3>
<P>
<center>
<form method="post" action="newtable.php">
<p>Table Name: <br><INPUT type ="text" name="table_name" size=30></p>
<p>
<INPUT type="submit" value="Continue">
</form>
</center>
</body>
</html>
Please help me
Thank you.