Jump to content

Adding A Database


iRush

Recommended Posts

Hey im trying to come up with a simple script that shows the databases in my localhost then create a insert box with a "submit button" and when i type in any name lets just say test1234 it should show up in the list and i want it to show the results below such as you successfully added a database. I have started this not too long ago but i've had experience with previous database adding and i just threw it in there to get me started. Here is my code as of now. By the way I am using PHP my admin and i have it so it creates it to the list but it does it automatically but i want it to add it by me typing it in the text box and when i hit the submit button it shows up in the list and below as well.

 

<?
$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>


<HTML>
<HEAD>
<TITLE>Adding a Database to MySQL</TITLE>
</HEAD>
<BODY><H3>
<FORM METHOD="post" ACTION="pretask.php">
<P>Database Name
<INPUT TYPE="text" NAME="val1" SIZE=10></P>
<P><INPUT TYPE="submit" NAME="submit" VALUE="Add database"></P>
</FORM>
</BODY></H3>
</HTML>
</font>


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

if ($connection) {
$msg = "YES!";
}


$sql = "CREATE DATABASE my_music ";
$result = @mysql_query($sql,$connection) or die(mysql_error())

?>


<HTML>
<BODY>

<h1><center><? echo "$msg"; ?></h1></center>

</BODY>
</HTML>

 

 

Link to comment
Share on other sites

Running this little script should get you started.

 

$connection = @mysql_connect("localhost", "root", "") or die(mysql_error());;
$dbs = @mysql_list_dbs($connection)or die(mysql_error());

while ($database = mysql_fetch_assoc($dbs)){
         echo "\n" . print_r($database) . "\n\n
";
}

 

or with fetch_array

$connection = @mysql_connect("localhost", "root", "") or die(mysql_error());;
$dbs = @mysql_list_dbs($connection)or die(mysql_error());

while ($database = mysql_fetch_array($dbs)){
         echo "\n" . print_r($database) . "\n\n
";
}

Link to comment
Share on other sites

Well i have a different way of doing it and this is my code as of so far. It adds it to my database it says it created it successfully but it doesnt automatically add it i have to hit the submit button again before i see it appear in my list but that is the least of my worries. Now i want to beable to type in a name and delete it can anyone help me as far as the deleting part?

 

This is my code up to date

 

<form action="db_list.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","Shawn","1234") 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)

?>

Link to comment
Share on other sites

I have this for my drop down list but how do i actually show all the databases i have to use the variable $db_list somewhere not too sure though. It just shows $db_list and not the actual list is there another way to make a drop down list instead of this way?

 

 

<html> 
<body> 
<form action="pretask.php" method="post"> 
<select> 
<option>$db_list</option> 
</select> 
<input type="submit" name="delete" value="delete"/> 
</form> 
</body> 
</html> 

Link to comment
Share on other sites

Probably something like this.  Modify the IF statement as needed in your case.

<html> 
<body> 
<form action="pretask.php" method="post"> 
<select>
<?PHP
$connection = mysql_connect("$host","$login","$pass") 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>".$row->Database."</option>";
 }
}
?> 
</select> 
<input type="submit" name="delete" value="delete"/> 
</form> 
</body> 
</html>

Link to comment
Share on other sites

Ok this helped me out alot this is my updated code with what you told me to add.. but this error came up..i tried choosing one of the databases i hit the delete button but it did not delete it. Here is the error:

 

Notice: Undefined index: database in C:\xampp\htdocs\pretask.php on line 42

 

 

 

 

<?
$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)

?>


<html>
<body>
<form action="pretask.php" method="post">
<select>
<?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>".$row->Database."</option>";
 }
}
?>
</select>
<input type="submit" name="delete" value="delete"/>
</form>

Link to comment
Share on other sites

Have you tried the version I posted?  The version below has the Select Name added and the values added for each option.

<html> 
<body>
<form action="pretask.php" method="post">
<select name="db">
<?PHP
$connection = mysql_connect("$host","$login","$pass") or die(mysql_error());;
$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>

Link to comment
Share on other sites

iRush

Yeah i did and when i select one and hit delete nothing happens it doesnt delete it from the list of databases. It comes up with that error though in the above post.

Drummin

The version below has the Select Name added and the values added for each option.

By adding print_r($POST) you can see values being passed on this new version.

Link to comment
Share on other sites

<html>

<body>

<?PHP

print_r($_POST);

?>

<form action="pretask.php" method="post">

<select name="db">

<?PHP

$connection = mysql_connect("$host","$login","$pass") or die(mysql_error());;

$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>

Link to comment
Share on other sites

It says this now

 

Database testdb6 has been added Array ( [database] => testdb6 [result] => Create )

 

And it still doesn't delete anything from the list. Thank you so much for helping out this far though I appreciate it alot! I just want to figure this out

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.