I found this script and i am trying to implement it into the admin section of my site.
I am having problems with the variable
$db_name
and
$fields
Basically it doesn't seem to like that they are not defined. I am confused as to how I can go about setting them so that when my form submits and brings that page up again it won't reset them at the beginning of the page everytime.
any help would be great. Maybe it is just too late and I need to call it a night.
<?PHP
###Includes###
include("../db/db_connect.php");
###End Includes###
$dbvar = mysql_list_dbs($db);
$db_list = NULL;
if (!isset($_POST['submit']))
{
$db_name = NULL;
$fields = NULL;
}
for($row = 0; $row < mysql_num_rows($dbvar); $row++)
{
$db_list .= mysql_tablename($dbvar, $row). "<br>";
}
?>
<html><head><title>DMP | Admin</title></head>
<body>
<h3> <?php echo($db_list); ?></h3>
<?php
if(!$fields && !$db_name)
{
$form = "<form action\"_SERVER['PHP_SELF']\" method =\"post\">";
$form .= "How Many fields are needed in the new table?";
$form .= "<br><input type =\"text\" name=\"fields\">";
$form .= "<input type=\"submit\" name= \"submit\" value=\"Submit\"></form>";
echo($form);
}
else if (!$db_name)
{
$form = "<form action\"_SERVER['PHP_SELF']\" method =\"post\">";
$form .="Database:<input type =\"text\" name = \"db_name\"></input><br>";
$form .="Table Name:<input type =\"text\" name = \"table\"></input><br>";
for ($i = 0; $i < $fields; $i++)
{
$form .= "Column Name:<input type =\"text\" name =\"name[$i]\"></inpput>";
$form .= "Type: <select name = \"type[$i]\">";
$form .= "<option value = \"CHAR\">char</option>";
$form .= "<option value = \"INT\">int</option>";
$form .= "<option value = \"TINYINT\">TINYINT</option>";
$form .= "<option value = \"SMALLINT\">SMALLINT</option>";
$form .= "<option value = \"MEDIUMINT\">MEDIUMINT</option>";
$form .= "<option value = \"BIGINT\">BIGINT</option>";
$form .= "<option value = \"FLOAT\">FLOAT</option>";
$form .= "<option value = \"DOUBLE\">DOUBLE</option>";
$form .= "<option value = \"DECIMAL\">DECIMAL</option>";
$form .= "<option value = \"VARCHAR\">VARCHAR</option>";
$form .= "<option value = \"TEXT\">TEXT</option>";
$form .= "<option value = \"SET\">SET</option>";
$form .= "<option value = \"DATETIME\">DATETIME</option>";
$form .= "<option value = \"TIMESTAMP\">TIMESTAMP</option>";
$form .= "<option value = \"YEAR\">YEAR</option></select>";
$form .= "Size:<input type =\"text\" name=\"size[$i]\"></input>";
}
$form .= "<input type = \"submit\" value =\"enter\"></input>";
$form .= "</form>"; echo($form);
}else
{
$sql = "CREATE TABLE $table (";
for ($i = 0; $i < count($name); $i++)
{
$sql .= "$name[$i] $type=[$i]";
if(($type[$i] == "CHAR") or ($type[$i] == "VARCHAR"))
{
if($size[$i] != "")
{
$sql .="($size[$i])";
}
}
if(($i+1) != count($name) )
{
$sql .= ",";
}
}
$sql .= ")";
echo ("SQL COMMAND: $sql <hr>");
$result = mysql_query($sql, $db) or die("Error:Query");
if($result)
{
echo ("RESULT: table \"$table\" has been created");
}
}
?>
</body>
</html>