Intro

OK, there's going to be a lot here, but it's just something that I built as my own crapped down version of PHPmyAdmin. I know it's not great. but that's not the point ;)

Getting started

db_management:

<html>
<body>
	<table border="0" width="100%" cellpadding="0" cellspacing="0">
	<tr>
		<td align="left" valign="top">
<?php $host = "localhost"; //Location of Mysql
$name = "michael"; //User Name for Mysql
$pass = ""; //Password for Mysql 
	$connect = mysql_connect ($host, $name, $pass)
				or die (mysql_error());
	$database = mysql_list_dbs ($connect)
				or die (mysql_error()); 	$database_list = "<ul>"; 	$db_value = 0; 	while ($db_value < mysql_num_rows ($database))
		 {
		 $database_names[$db_value] = mysql_tablename ($database, $db_value);
		 $database_list .= "<li>$database_names[$db_value]";
			 $tables = mysql_list_tables ($database_names[$db_value]) or die (mysql_error());
			 $tb_list = "<ul>";
			 $tb_value = 0;
				 while ($tb_value < mysql_num_rows($tables))
					 {
						$table_names[$tb_value] = mysql_tablename ($tables, $tb_value);
						$tb_list .= "<li>$table_names[$tb_value]";
						$tb_value++;
					 }
		 $tb_list .= "</ul><br />";
		 $database_list .= "$tb_list";
		 $db_value++;
		 }
 
	$database_list .= "</ul>";
	echo "<b>Database Management</b>";
	echo "$database_list"; ?> 		</td>
		<td algin="left" valign="top">
	<strong>
		Please type the name of the database you wish to modify!
	</strong>
	<form method="post" action="db_modify.php"> 	<input type="text" name="DBname" id="DBname"><i> ---enter database name as seen</i>
	<br />
	<input type="submit" name="submit" id="sumbit" value="Modify Database"> 	</form>
	<br />
	<br />
	<strong>
		Please type the name of the database you wish to Create!
	</strong>
	<form method="post" action="db_create.php"> 	<input type="text" name="DBname" id="DBname">
	<br />
	<input type="submit" name="submit" id="sumbit" value="Create Database"> 	</form>
		</td>
</body>
</html>

Modifying the databases

db_modify:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Modify Database</title>
</head>
 
<body>
	<table border="1" width="100%" cellpadding="0" cellspacing="0">
	<tr>
		<td align="left" valign="top">
		<?php
 
		$host = "localhost"; //Location of Mysql
		$name = "michael"; //User Name for Mysql
		$pass = ""; //Password for Mysql
		$DBname = " $_ POST[DBname]"; //Name of the database to be created in Mysql
 
mysql_connect($host,$name,$pass) or die("Unable to connect to database"); //connecting to the database using the variable set
mysql_select_db("$DBname") or die("Unable to select database $DBname<br /><a href=\"<A href="http://localhost/dbadmin/db_management.php/">Return">http://localhost/dbadmin/db_management.php\">Return to Database Management</a>"); //at connection to the databse select DBNAME (phpforms) or tell that it couldnt connect
 
$tables = mysql_list_tables ($DBname) or die (mysql_error());
$TBlist = "<ul>";
$TBvalue = 0;
while ($TBvalue < mysql_num_rows($tables)) // while the value is less than the number of rows in current database
{
$table_names[$TBvalue] = mysql_tablename ($tables, $TBvalue); //display current name based on value 
$TBlist .= "<li>$table_names[$TBvalue]"; //output the current name
$TBvalue++; //add 1 to the current value of TBvalue
}
$TBlist .= "</ul>";
echo "Tables located in $DBname";
echo $TBlist;
 
if ($DBname == "mysql")
{
echo"</td>
<td algin=\"left\" valign=\"top\">
<strong>
Cannot Remove $DBname!
</strong>
<br />
Form Action to delete this database has been removed!
</form>
<br />
<br />
<strong>
Please type the name of the Table you wish to delete! 
</strong>
<form method=\"post\" action=\"tb_remove.php\">
 
<input type=\"text\" name=\"TBname\" id=\"TBname\">
<input type=\"hidden\" name=\"DBname\" id=\$DBname\" value\"$DBname\">
<br />
<input type=\"submit\" name=\"submit\" id=\"sumbit\" value=\"Remove Table from Database\">
 
</form>
<br />
<br />
<strong>
Please type the name of the Table you wish to Create!
</strong>
<form method=\"post\" action=\"tb_add.php\">
 
<input type=\"text\" name=\"TBname\" id=\"TBname\">
<input type=\"text\" maxlength=\"4\" size=\"4\" name=\"rows\" id=\"rows\">
<input type=\"hidden\" name=\"DBname\" id=\$DBname\" value\"$DBname\">
<br />
<input type=\"submit\" name=\"submit\" id=\"sumbit\" value=\"Add Table into Database\">
 
</form>
</td>
<tr>
<td colspan=\"2\" align=\"center\">
<a href=\"<A href="http://localhost/dbadmin/db_management.php/">Return">http://localhost/dbadmin/db_management.php\">Return to Database Management</a>";
}
else
{
echo "
</td>
<td algin=\"left\" valign=\"top\">
<strong>
Click to remove the current Database!
</strong>
<form method=\"post\" action=\"db_remove.php\">
 
<input type=\"hidden\" name=\"DBname\" id=\"DBname\" value=\"$DBname\">
<i>Warning: This cannot be undone!</i><br />
<input type=\"submit\" name=\"submit\" id=\"sumbit\" value=\"Delete Database\">
 
</form>
<br />
<br />
<strong>
Please type the name of the Table you wish to delete!
</strong>
<form method=\"post\" action=\"tb_remove.php\">
 
<input type=\"text\" name=\"TBname\" id=\"TBname\">
<input type=\"hidden\" name=\"DBname\" id=\"DBname\" value=\"$DBname\">
<br />
<input type=\"submit\" name=\"submit\" id=\"sumbit\" value=\"Remove Table from Database\">
 
</form>
<br />
<br />
<strong>
Please type the name of the Table you wish to Create!<br />
</strong>
Table Name / # of Fields / Database for modifacations 
<form method=\"post\" action=\"tb_add.php\">
 
<input type=\"text\" name=\"TBname\" id=\"TBname\">
<input type=\"text\" maxlength=\"4\" size=\"4\" name=\"rows\" id=\"rows\">
<input type=\"text\" name=\"DBname\" id=\"DBname\" value=\"$DBname\" size=\"10\"><i> -- Warning: do not modify if you wish to make changes to current Database!</i>
<br />
<input type=\"submit\" name=\"submit\" id=\"sumbit\" value=\"Add Table into Database\">
 
</form>
</td>
<tr>
<td colspan=\"2\" align=\"center\">
<a href=\"<A href="http://localhost/dbadmin/db_management.php/">Return">http://localhost/dbadmin/db_management.php\">Return to Database Management</a>";
}
?>
</body>
</html>

Database Creation

db_create:

<html>
<body>
<?php
$host = "localhost"; //location of the mysql
$name = "michael"; //user name for logging into mysql
$pass = ""; //password for logging into mysql
 
$DBname = " $_POST[DBname]"; //name of the database for creation
 
mysql_connect ($host, $name, $pass) or die(mysql_error());
$query = "CREATE DATABASE $DBname";
mysql_query($query) or die(mysql_error()); 
 
echo "<b>Database $DBname Has been Created!</b><br />";
echo "<a href=\"<A href="http://localhost/dbadmin/db_management.php/">Return">http://localhost/dbadmin/db_management.php\">Return to Database Management</a>";
 
mysql_close();
?>
</body>
</html>

Database removal

db_remove:

<html>
<body>
<?php
$host = "localhost"; //location of the mysql
$name = "michael"; //user name for logging into mysql
$pass = ""; //password for logging into mysql
 
$DBname = "$_POST[DBname]"; //name of the database for deletion
 
mysql_connect ($host, $name, $pass) or die(mysql_error());
$query = "DROP DATABASE $DBname";
mysql_query($query) or die(mysql_error()); 
 
echo "<b>Database $DBname Has been Deleted!</b><br />";
echo "<a href=\"<A href="http://localhost/dbadmin/db_management.php/">Return">http://localhost/dbadmin/db_management.php\">Return to Database Management</a>";
 
mysql_close();
?>
</body>
</html>

Table add

tb_add:

<?php
 
$DBname = "$_POST[DBname]"; // name of the database that you are currently working in
$TBname = "$_POST[TBname]"; //name of the table for Creation
$rows = "$_POST[rows]"; //the number of rows in a particular table
$form_block = "<form method=\"post\" action=\"tb_create.php\">
<input type=\"hidden\" name=\"TBname\" id=\"TBname\" value=\"$TBname\">
<input type=\"hidden\" name=\"DBname\" id=\"DBname\" value=\"$DBname\">
<Table cellspacing=\"5\" cellpading=\"5\">
<tr>
<th>Field Name</th><th>Field Type</th><th>Field Length</th><th>Primary Key?</th><th>AUTO-INCREMENT?</th></tr>";
 
//count up from 0 untill number of fields is == to the value specified
for ($count = 0; $count < $rows; $count++)
{
$form_block .= "<tr> 
<td align=\"center\">
<input type=\"text\" name=\"field[]\" id=\"feild[]\" size=\"30\">
</td>
<td align=\"center\">
<select name=\"type[]\" id=\"type[]\">
<option value=\"char\">char</option>
<option value=\"date\">date</option>
<option value=\"float\">float</option>
<option value=\"int\">int</option>
<option value=\"text\">text</option>
<option value=\"varchar\">varchar</option>
</select>
</td>
<td align=\"center\">
<input type=\"text\" name=\"length[]\" id=\"length[]\" size=\"5\">
</td>
</td>
<td align=\"center\">
<input type=\"checkbox\" name=\"primary[]\" id=\"primary[]\" value=\"y\">
</td> 
<td align=\"center\">
<input type=\"checkbox\" name=\"auto_increment[]\" id=\"auto_increment[]\" value=\"y\">
</td>
</tr>";
}
$form_block .= "<tr>
<td align=\"center\" colspan=\"3\">
<input type=\"submit\" value=\"Create Table\">
</td>
</tr>
</table>
</form>";
echo $DBname . "<br />";
echo "Define Fields for $TBname";
echo "$form_block";
echo "<a href=\"<A href="http://localhost/dbadmin/db_management.php/">Return">http://localhost/dbadmin/db_management.php\">Return to Database Management</a>"; 
 
?>

Table removing

tb_remove:

<?php
$host = "localhost"; //location of the mysql
$name = "michael"; //user name for logging into mysql
$pass = ""; //password for logging into mysql
 
$DBname = "$_POST[DBname]"; //name of the database which is having the table removed
$TBname = "$_POST[TBname]"; //name of the table for deletion
 
mysql_connect ($host, $name, $pass) or die(mysql_error());
$query = "USE $DBname";
$query2 = "DROP TABLE $TBname";
mysql_query($query) or die(mysql_error());
mysql_query($query2) or die(mysql_error() . "<br />" . "<a href=\"<A href="http://localhost/dbadmin/db_management.php/">Return">http://localhost/dbadmin/db_management.php\">Return to Database Management</a>");
 
echo "<b>Table $TBname Has been Deleted From Database $DBname!</b><br />";
echo "<a href=\"<A href="http://localhost/dbadmin/db_management.php/">Return">http://localhost/dbadmin/db_management.php\">Return to Database Management</a>";
 
mysql_close();
?>

Conclusion

Thats about it, I've written other things but this is what I'm currently working on.

I'm also working on a mysql database tool that will allow the use to edit it in real time, but that's to come later on...much later on.

would you be able to post the tb_create.php code

great post by the way :) :)

I think there were some issues with tb_create, mostly I hadn't gotten it working at the time lol.

but you are more than welcome to have at it. This is a pretty old thread so you should count your lucky stars I even found the source for this!!

<?php
$host = "localhost"; //location of the mysql
$name = ""; //user name for logging into mysql
$pass = ""; //password for logging into mysql

$TBname = "$_POST[TBname]"; //name of the table for Creation
$DBname = "$_POST[DBname]"; //name of the database for working in


    $connect = mysql_connect($host, $name, $pass) or die(mysql_error());
    mysql_select_db($DBname) or die(mysql_error());
    
    $sql = "CREATE TABLE $TBname ("; //creates the table

    for ($count = 0; $count < count($_POST[field]); $count++)
       {
        $sql .= $_POST[field][$count] . " " . $_POST[type][$count];
            
            if ($_POST[auto_increment][$count]  == "y")
              {
               $additional = "NOT NULL AUTO_INCREMENT";
              }
            else
                {
                 $additional = "";
                }
            if ($_POST[primary][$count] == "y")
              {
               $additional .= ", primary key (" . $_POST[field][$count] . ")";
              }
            else
                {
                 $additional = "";
                }
            if ($_POST[length][$count] != "")
              {
               $sql .= " (" .$_POST[length][$count] . ") $additional ,";
              }
            else
                {
                 $sql .= " $additional ,";
                }
       }
// clean up the end of the string
$sql .= ")";

$result = mysql_query($sql, $connect) or die(mysql_error()); // execute the query

if ($result)
  {
   $msg = "<p>" . $TBname . "has been created</p>";
  }
echo "Adding $TBname to $DBname ....Done<br />";
echo "Creating fields in $TBname ....Done<br />";
echo $msg . "<br />"; 
echo "<a href=\"http://localhost/dbadmin/db_management.php\">Return to Database Management</a>";
?>

thank you for replying, good code :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.