Database Administration (Cont.)
May 24, 2002
The next step is to include the chapter5_checklogin.php script to get the user's login credentials or ask them to log in if they have not already done so:
include ('chapter5_checklogin.php');
Once the user is logged in, you present them with a list of available submenu choices to choose from:
//If logged in, give list of dbAdmin actions if ($loggedin == TRUE) { echo "<CENTER>\n"; echo "<A HREF=\"chapter5_dbAdmin.php?action=list\""; echo ">List Databases</A>\n"; echo "<A HREF=\"chapter5_dbAdmin.php?action=select1\""; echo ">Select Database</A>\n"; echo "<A HREF=\"chapter5_dbAdmin.php?action=drop1\""; echo ">Drop Database</A>\n"; echo "<A HREF=\"chapter5_dbAdmin.php?action=create1\""; echo ">Create Database</A>\n"; echo "</CENTER>\n"; }
Similar to chapter5_dbMonitor.php code, the links direct the user back to this page with a particular action specified. For each of the actions, you are going to need to call a method of the dbAdmin class, so you next create a dbAdmin object:
$myDBAdmin = new dbAdmin($myDBConn->linkid);
Then you call the appropriate method based on the passed-in action value:
switch($userinfo[action]) { case 'list': { $dblistarray = $myDBAdmin->listDBs(); displayArray("Databases",$dblistarray); break; }
If the user needs to select a database, you'll need to ask more information— namely, what database they want to select—before you can invoke the selectDB() method of the dbAdmin class. To do this, output a single line form and direct the user back to this page with an action of select2 indicating that the database to select has been named, and now you are ready to call the selectDB() method, as shown here:
case 'select1': { echo "<CENTER>\n"; echo "<FORM ACTION=\"chapter5_dbAdmin.php\""; echo "METHOD=\"POST\">\n"; echo "DB Name <INPUT TYPE=\"TEXT\""; echo "NAME=\"in_database\""; echo "SIZE=\"30\">\n"; echo "<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\""; echo "select2\" SIZE=\"30\">\n"; echo "<INPUT TYPE=\"submit\" VALUE=\"Select DB\">\n"; echo "</FORM>\n"; echo "</CENTER>\n"; break; } case 'select2': { $rc = $myDBAdmin->selectDB($userinfo[in_database]); echo "<BR><BR>\n"; echo "<CENTER>\n"; if ($rc ==TRUE) { echo "Database changed to ".$userinfo[in_database]."."; $sessiondata[database] = $userinfo[in_database]; } else { echo "Database could not be changed to "; echo $userinfo[in_database]."."; } echo "</CENTER>\n"; break; }
A similar situation occurs for both Drop and Create Database. You must first identify which database the user wants to create or drop and then call the appropriate dbAdmin method, as follows:
case 'drop1': { echo "<CENTER>\n"; echo "<FORM ACTION=\"chapter5_dbAdmin.php\""; echo "METHOD=\"POST\">\n"; echo "DB Name <INPUT TYPE=\"text\" NAME=\""; echo "in_dropdatabase\" SIZE=\"30\">\n"; echo "<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\"drop2\""; echo "SIZE=\"30\">\n"; echo "<INPUT TYPE=\"submit\" VALUE=\"Drop DB\">\n"; echo "</FORM>\n"; echo "<CENTER>\n"; break; } case 'drop2': { $rc = $myDBAdmin->dropDB($userinfo[in_dropdatabase]); echo "<BR><BR>\n"; echo "<CENTER>\n"; if ($rc ==TRUE) { echo "Database ".$userinfo[in_dropdatabase]." was dropped."; } else { echo "Database ".$userinfo[in_dropdatabase].
" could not be dropped."; } echo "</CENTER>\n"; break; } case 'create1': { echo "<CENTER>\n"; echo "<FORM ACTION=\"chapter5_dbAdmin.php\" METHOD=\"POST\">\n"; echo "DB Name <INPUT TYPE=\"text\" NAME=\""; echo "in_createdatabase\" SIZE=\"30\">\n"; echo "<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\""; echo "create2\" SIZE=\"30\">\n"; echo "<INPUT TYPE=\"submit\" VALUE=\"Create DB\">\n"; echo "</FORM>\n"; echo "<CENTER>\n"; break; } case 'create2': { $rc = $myDBAdmin->createDB($userinfo[in_createdatabase]); echo "<BR><BR>\n"; echo "<CENTER>\n"; if ($rc ==TRUE) { echo "Database ".$userinfo[in_createdatabase]; echo " was created."; } else { echo "Database ".$userinfo[in_createdatabase]; echo " could not be created."; } echo "</CENTER>\n"; break; } } ?> </BODY> </HTML>
Note:Color coded lines have been split for display purposes
Stop by in one week for the next installment!
Database Administration
Instant PHP 4
Table Administration
|