Jump to content

Adding A Database


iRush

Recommended Posts

Yeah here is my full code and i have a create script right above that im not sure is 100% right or not..

 

<?
$connection = @mysql_connect("localhost", "root", "") or die(mysql_error());;

$dbs = @mysql_list_dbs($connection)or die(mysql_error());
$db_list="<ul>";
$i =0;

while ($i < mysql_num_rows($dbs)){
    $db_names[$i] = mysql_tablename($dbs,$i);

    $db_list .="<li>$db_names[$i]";

    $i++;

}

$db_list .="</ul>";
?>

<HTML>
<HEAD>
<TITLE>MySQL Databases</TITLE>
</HEAD>
<P><strong>Databases on localhost</strong>:</p>



<? echo "$db_list"; ?>

</BODY>
</HTML>
<form action="pretask.php" method="post">
Create Database <input type="text" name="database" />
<input type="submit" name="result" value="Create" />
</form>
</body>
</html>

<?php

$database=$_POST['database'];
$connection = @mysql_connect("localhost","root","") or die(mysql_error());
$sql="CREATE DATABASE $database ";
$result = @mysql_query($sql,$connection) or die(mysql_error());

if (isset($_POST['result']))
{
Results:
echo "Database $database has been added";
}
mysql_close($connection)

?>


<?PHP
print_r($_POST);
?>
<form action="pretask.php" method="post">
<select name="db">
</html>
<?PHP
$connection = mysql_connect("localhost","root","") or die(mysql_error());;
$db_list = mysql_list_dbs($connection);

while ($row = mysql_fetch_object($db_list)) {
if ($row->Database!="information_schema" && $row->Database!="mysql" && $row->Database!="phpmyadmin"){
echo "<option value=\"".$row->Database."\">".$row->Database."</option>";
 }
}
?>
</select>
<input type="submit" name="delete" value="delete"/>
</form>

Link to comment
Share on other sites

Sorry I missed that.  Must be going blind.  Try this.

<?PHP 
// please add your login and pass here//
$host	= "localhost";
$login 	= "" ;
$pass 	= "";

mysql_connect("$host","$login","$pass") OR DIE
        ("There is a problem with the system.  Please notify your system administrator." .mysql_error());

//Seems in this case we can use a general call
$connection = mysql_connect("$host","$login","$pass") or die(mysql_error());

//Start Create DB//
IF (isset($_POST['result'])){
$database=$_POST['database'];
$sql="CREATE DATABASE $database ";
$result = mysql_query($sql,$connection) or die(mysql_error());
}

IF (isset($_POST['delete'])){
$db=$_POST['db'];
$query=mysql_query("DROP DATABASE $db");
}
?> 
<html>
<head>
<title>MySQL Databases</title>
</head> 
<body>
<p><strong>Databases on localhost</strong>:</p>
<?PHP
//print_r($_POST);
?>
<form action="pretask.php" method="post">
<select name="db">
<?PHP
$db_list = mysql_list_dbs($connection);
while ($row = mysql_fetch_object($db_list)) {
//Here you are listing anything that should not be included
if ($row->Database!="information_schema" && $row->Database!="mysql" && $row->Database!="phpmyadmin"){
echo "<option value=\"".$row->Database."\">".$row->Database."</option>"; 
 }
}
?> 
</select> 
<input type="submit" name="delete" value="Delete"/> 
</form>
<form action="pretask.php" method="post">
Create Database <input type="text" name="database" />
<input type="submit" name="result" value="Create" />
</form>
</body>
</html>

Link to comment
Share on other sites

Ok the code you gave me works mint..but when i create and delete databases i have to refresh the page once or more to see the results. Any ideas?
See if this works better.
<?PHP 
$host	= "localhost";
$login 	= "";
$pass 	= "";

mysql_connect("$host","$login","$pass") OR DIE
        ("There is a problem with the system.  Please notify your system administrator." .mysql_error());

//Start Create DB//
IF (isset($_POST['result'])){
$database=$_POST['database'];
$connection = mysql_connect("$host","$login","$pass") or die(mysql_error());
$sql="CREATE DATABASE $database ";
$result = mysql_query($sql,$connection) or die(mysql_error());
}

IF (isset($_POST['delete'])){
$db=$_POST['db'];
$query=mysql_query("DROP DATABASE $db");
}
?> 
<html>
<head>
<title>MySQL Databases</title>
</head> 
<body>
<p><strong>Databases on localhost</strong>:</p>
<?PHP
//print_r($_POST);
?>
<form action="pretask.php" method="post">
<select name="db">
<?PHP
$connection2 = mysql_connect("$host","$login","$pass") or die(mysql_error());
$db_list = mysql_list_dbs($connection2);
while ($row = mysql_fetch_object($db_list)) {
//Here you are listing anything that should not be included
if ($row->Database!="information_schema" && $row->Database!="mysql" && $row->Database!="phpmyadmin"){
echo "<option value=\"".$row->Database."\">".$row->Database."</option>"; 
 }
}
?> 
</select> 
<input type="submit" name="delete" value="Delete"/> 
</form>
<form action="pretask.php" method="post">
Create Database <input type="text" name="database" />
<input type="submit" name="result" value="Create" />
</form>
</body>
</html>

Link to comment
Share on other sites

Well that's a pain.  Let's move processing to another page called processpretask.php.  processpretask.php will do the form processing and direct back to the first page.  Here's the code.

 

processpretask.php

<?PHP 
$host	= "localhost";
$login 	= "";
$pass 	= "";

mysql_connect("$host","$login","$pass") OR DIE
        ("There is a problem with the system.  Please notify your system administrator." .mysql_error());

//Start Create DB//
IF (isset($_POST['result'])){
$database=$_POST['database'];
$connection = mysql_connect("$host","$login","$pass") or die(mysql_error());
$sql="CREATE DATABASE $database ";
$result = mysql_query($sql,$connection) or die(mysql_error());
header("location: pretask.php");
}

IF (isset($_POST['delete'])){
$db=$_POST['db'];
$query=mysql_query("DROP DATABASE $db");
header("location: pretask.php");
}
?>

 

The pretask.php page doesn't process anymore and the forms point to the other page.

 

pretask.php

<?PHP 
$host	= "localhost";
$login 	= "";
$pass 	= "";

mysql_connect("$host","$login","$pass") OR DIE
        ("There is a problem with the system.  Please notify your system administrator." .mysql_error());

?> 
<html>
<head>
<title>MySQL Databases</title>
</head> 
<body>
<p><strong>Databases on localhost</strong>:</p>
<?PHP
//print_r($_POST);
?>
<form action="processpretask.php" method="post">
<select name="db">
<?PHP
$connection2 = mysql_connect("$host","$login","$pass") or die(mysql_error());
$db_list = mysql_list_dbs($connection2);
while ($row = mysql_fetch_object($db_list)) {
//Here you are listing anything that should not be included
if ($row->Database!="information_schema" && $row->Database!="mysql" && $row->Database!="phpmyadmin"){
echo "<option value=\"".$row->Database."\">".$row->Database."</option>"; 
 }
}
?> 
</select> 
<input type="submit" name="delete" value="Delete"/> 
</form>
<form action="processpretask.php" method="post">
Create Database <input type="text" name="database" />
<input type="submit" name="result" value="Create" />
</form>
</body>
</html>

Link to comment
Share on other sites

I'll try it tomorrow..will the database follow over to the next page as well?
No.  Every page you create needs these DB connection  variables defined.  Be sure to add the login and password on both pages.  Many folks make a common page that you include at the top of every page.  Say I call the page access.php and on this page I put the following.

<?PHP 
$host	= "localhost";
$login 	= "myusername";
$pass 	= "mypassword";
?>

You can add more common things like session_start(); if you are using sessions etc.

 

Then on any page you make you include this page.

<?PHP
require("access.php");
// etc. with the rest of your code.
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.