iRush Posted October 19, 2011 Share Posted October 19, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/ Share on other sites More sharing options...
iRush Posted October 19, 2011 Author Share Posted October 19, 2011 Anyone have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280417 Share on other sites More sharing options...
iRush Posted October 19, 2011 Author Share Posted October 19, 2011 no one???? Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280569 Share on other sites More sharing options...
iRush Posted October 19, 2011 Author Share Posted October 19, 2011 ........ Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280593 Share on other sites More sharing options...
Zane Posted October 19, 2011 Share Posted October 19, 2011 Since mysql_list_dbs returns a MySQL resource pointer, you should be using mysql_fetch_array or mysql_fetch_assoc in your while loop, instead of mysql_num_rows. while ($database = mysql_fetch_assoc($dbs)){ Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280602 Share on other sites More sharing options...
Zane Posted October 19, 2011 Share Posted October 19, 2011 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 "; } Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280603 Share on other sites More sharing options...
iRush Posted October 19, 2011 Author Share Posted October 19, 2011 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) ?> Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280619 Share on other sites More sharing options...
xyph Posted October 19, 2011 Share Posted October 19, 2011 The @ operator suppresses errors. You generally don't want to do that, especially when things are going wrong. See if mysql_error returns anything. Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280624 Share on other sites More sharing options...
iRush Posted October 19, 2011 Author Share Posted October 19, 2011 Can you help me out with the delete part..im not too concerned about the creating part at this time? Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280627 Share on other sites More sharing options...
xyph Posted October 19, 2011 Share Posted October 19, 2011 Oh sorry, I misread your post. $query = 'DROP DATABASE IF EXISTS dbname'; http://dev.mysql.com/doc/refman/5.0/en/drop-database.html Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280630 Share on other sites More sharing options...
iRush Posted October 19, 2011 Author Share Posted October 19, 2011 Where would i put this in my code and i would have to make another submit button and add a value to it just like i did for the create button. Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280632 Share on other sites More sharing options...
xyph Posted October 19, 2011 Share Posted October 19, 2011 I don't provide copy and paste solutions. Please understand your code before using it. Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280663 Share on other sites More sharing options...
iRush Posted October 20, 2011 Author Share Posted October 20, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280688 Share on other sites More sharing options...
trq Posted October 20, 2011 Share Posted October 20, 2011 You need to use <?php ?> tags around php. Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280690 Share on other sites More sharing options...
Drummin Posted October 20, 2011 Share Posted October 20, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280691 Share on other sites More sharing options...
iRush Posted October 20, 2011 Author Share Posted October 20, 2011 Is there a shortcut instead of writing out all the databases in my if statement? Would it be easier to make another text field and use a delete "submit button" to delete the databases? Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280694 Share on other sites More sharing options...
iRush Posted October 20, 2011 Author Share Posted October 20, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280697 Share on other sites More sharing options...
Drummin Posted October 20, 2011 Share Posted October 20, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280704 Share on other sites More sharing options...
iRush Posted October 20, 2011 Author Share Posted October 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280705 Share on other sites More sharing options...
Drummin Posted October 20, 2011 Share Posted October 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280709 Share on other sites More sharing options...
iRush Posted October 20, 2011 Author Share Posted October 20, 2011 Where would i add print_r($POST) if you dont mind me asking Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280710 Share on other sites More sharing options...
Drummin Posted October 20, 2011 Share Posted October 20, 2011 <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> Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280712 Share on other sites More sharing options...
iRush Posted October 20, 2011 Author Share Posted October 20, 2011 Do i have to change the "echo option value at the bottom? Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280716 Share on other sites More sharing options...
iRush Posted October 20, 2011 Author Share Posted October 20, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280717 Share on other sites More sharing options...
Drummin Posted October 20, 2011 Share Posted October 20, 2011 What's you current full page? Looking at what's posted you have [database] => testdb6 [result] => Create, so I assume you are triggering a "Create" DB script. Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/#findComment-1280721 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.