943,837 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3649
  • PHP RSS
Jul 15th, 2004
0

Custom Database Admin Tool.

Expand Post »
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:

[PHP] <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> [/PHP]


Modifying the databases

db_modify:

[PHP]
<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>
[/PHP]

Database Creation

db_create:

[PHP]
<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>
[/PHP]


Database removal

db_remove:

[PHP] <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>
[/PHP]

Table add

tb_add:

[PHP] <?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>";

?>

[/PHP]

Table removing

tb_remove:

[PHP] <?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();
?>
[/PHP]


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.
Last edited by happygeek; Oct 29th, 2006 at 10:01 am. Reason: Formatting
Similar Threads
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Apr 26th, 2009
0

Re: Custom Database Admin Tool.

would you be able to post the tb_create.php code

great post by the way
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pansylea is offline Offline
2 posts
since Apr 2009
May 5th, 2009
0

Re: Custom Database Admin Tool.

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 Syntax (Toggle Plain Text)
  1. <?php
  2. $host = "localhost"; //location of the mysql
  3. $name = ""; //user name for logging into mysql
  4. $pass = ""; //password for logging into mysql
  5.  
  6. $TBname = "$_POST[TBname]"; //name of the table for Creation
  7. $DBname = "$_POST[DBname]"; //name of the database for working in
  8.  
  9.  
  10. $connect = mysql_connect($host, $name, $pass) or die(mysql_error());
  11. mysql_select_db($DBname) or die(mysql_error());
  12.  
  13. $sql = "CREATE TABLE $TBname ("; //creates the table
  14.  
  15. for ($count = 0; $count < count($_POST[field]); $count++)
  16. {
  17. $sql .= $_POST[field][$count] . " " . $_POST[type][$count];
  18.  
  19. if ($_POST[auto_increment][$count] == "y")
  20. {
  21. $additional = "NOT NULL AUTO_INCREMENT";
  22. }
  23. else
  24. {
  25. $additional = "";
  26. }
  27. if ($_POST[primary][$count] == "y")
  28. {
  29. $additional .= ", primary key (" . $_POST[field][$count] . ")";
  30. }
  31. else
  32. {
  33. $additional = "";
  34. }
  35. if ($_POST[length][$count] != "")
  36. {
  37. $sql .= " (" .$_POST[length][$count] . ") $additional ,";
  38. }
  39. else
  40. {
  41. $sql .= " $additional ,";
  42. }
  43. }
  44. // clean up the end of the string
  45. $sql .= ")";
  46.  
  47. $result = mysql_query($sql, $connect) or die(mysql_error()); // execute the query
  48.  
  49. if ($result)
  50. {
  51. $msg = "<p>" . $TBname . "has been created</p>";
  52. }
  53. echo "Adding $TBname to $DBname ....Done<br />";
  54. echo "Creating fields in $TBname ....Done<br />";
  55. echo $msg . "<br />";
  56. echo "<a href=\"http://localhost/dbadmin/db_management.php\">Return to Database Management</a>";
  57. ?>
Last edited by Killer_Typo; May 5th, 2009 at 11:03 pm.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
May 6th, 2009
0

Re: Custom Database Admin Tool.

thank you for replying, good code
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pansylea is offline Offline
2 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: find solution in minimum time??
Next Thread in PHP Forum Timeline: Alert box after success submit form validate





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC