iRush Posted October 20, 2011 Author Share Posted October 20, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/page/2/#findComment-1280722 Share on other sites More sharing options...
Drummin Posted October 20, 2011 Share Posted October 20, 2011 Well I can't help you if that's all you going to provide. I don't see the code I provided or any forms or processing code. Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/page/2/#findComment-1280724 Share on other sites More sharing options...
iRush Posted October 20, 2011 Author Share Posted October 20, 2011 Did you scroll down through the whole code...i added what you told my to..to my code that i already had started Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/page/2/#findComment-1280726 Share on other sites More sharing options...
iRush Posted October 20, 2011 Author Share Posted October 20, 2011 ^^^^^^^^^ anyone? Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/page/2/#findComment-1280734 Share on other sites More sharing options...
Drummin Posted October 20, 2011 Share Posted October 20, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/page/2/#findComment-1280735 Share on other sites More sharing options...
Drummin Posted October 20, 2011 Share Posted October 20, 2011 Remember to not use short tags "<?". Use full tags "<?PHP". Remember to wrap any processing scripts in an IF statement, otherwise they are always called when the page loads regardless if you are adding or deleting data. Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/page/2/#findComment-1280737 Share on other sites More sharing options...
iRush Posted October 20, 2011 Author Share Posted October 20, 2011 I need my original list to show up somehow but no databases are showing up. Also the delete dropdown list isnt showing databases either probably because none are being show Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/page/2/#findComment-1280739 Share on other sites More sharing options...
iRush Posted October 20, 2011 Author Share Posted October 20, 2011 Do I need this in the code while ($i < mysql_num_rows($dbs)){ $db_names[$i] = mysql_tablename($dbs,$i); $db_list .="<li>$db_names[$i]"; $i++; $db_list .="</ul>" or this <? echo "$db_list"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/page/2/#findComment-1280741 Share on other sites More sharing options...
iRush Posted October 20, 2011 Author Share Posted October 20, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/page/2/#findComment-1280746 Share on other sites More sharing options...
Drummin Posted October 20, 2011 Share Posted October 20, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/page/2/#findComment-1280747 Share on other sites More sharing options...
iRush Posted October 20, 2011 Author Share Posted October 20, 2011 Didnt change anything..still have to refresh. This is a tricky one to figure out Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/page/2/#findComment-1280753 Share on other sites More sharing options...
Drummin Posted October 20, 2011 Share Posted October 20, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/page/2/#findComment-1280755 Share on other sites More sharing options...
iRush Posted October 20, 2011 Author Share Posted October 20, 2011 I'll try it tomorrow..will the database follow over to the next page as well? Thanks bud for all your help so far..you've been great! Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/page/2/#findComment-1280756 Share on other sites More sharing options...
Drummin Posted October 20, 2011 Share Posted October 20, 2011 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. ?> Quote Link to comment https://forums.phpfreaks.com/topic/249366-adding-a-database/page/2/#findComment-1280760 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.