0

我已经有这个问题 3 天了 :( 我通过 phpmyadmin 创建了一个 3 个数据库,并且我已经将这 3 个数据库放入了一个下拉列表中,我想要的是,当我点击提交时,默认数据库会根据下拉列表的值进行更改列表

这是我想出的代码

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form2" method="post" >
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><center><strong>Select Default Database</strong></center></td>
</tr>
<tr>
<td><center><select name="db_used">
<?php 
$host="localhost";
$username="root";
$password="";

$link = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
$result = mysql_list_dbs( $link );

while( $row = mysql_fetch_object( $result ) ){
echo "<option>$row->Database</option>";
}
?>

</select></center></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>



 <?php
$host="localhost";
$username="root";
$password="";
$db_name = "#this depends on the value of the dropdown list"; 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
?>

任何帮助都非常感谢~~

4

1 回答 1

0

希望这会对您有所帮助。您可以这样做:

创建 index.php

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form2" method="post" action='generate.php'>
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><center><strong>Select Default Database</strong></center></td>
</tr>
<tr>
<td><center><select name="db_used">
<?php 
$host="localhost";
$username="root";
$password="";

$link = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
$result = mysql_list_dbs( $link );

while( $row = mysql_fetch_object( $result ) ){
echo "<option>$row->Database</option>";
}
?>

</select></center></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>

箱生成.php

<?php
$HOMEDIR = getcwd() . "/";
# /home/virtual/magicplugin.com/var/www/html/
$TEMPLATEDIR = $HOMEDIR . "";

$fName = $TEMPLATEDIR . "dbConnection.php";
    $fh = fopen($fName, "r");   $fContents = fread($fh, filesize($fName));  fclose($fh);
    $fContents = str_replace("__databaseName__",$_REQUEST['db_used'],$fContents);
    $outFile ="index.php";
    $fp = fopen($outFile,"w");
        fwrite($fp,$fContents); fclose($fp);

        echo"Connection generated.Please Check";
        ?>

创建 dbConnection.php

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form2" method="post" action='generate.php'>
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><center><strong>Select Default Database</strong></center></td>
</tr>
<tr>
<td><center><select name="db_used">
<?php 
$host="localhost";
$username="root";
$password="";

$link = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
$result = mysql_list_dbs( $link );

while( $row = mysql_fetch_object( $result ) ){
echo "<option>$row->Database</option>";
}
?>

</select></center></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>



 <?php
$host="localhost";
$username="root";
$password="";
$db_name = "__databaseName__"; 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
?>
于 2012-11-10T22:44:20.953 回答