更改sqldatabase php脚本

时间:2012-11-10 20:49:31

标签: php mysql

我已经有这个问题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");
?>

任何帮助都非常感谢~~

1 个答案:

答案 0 :(得分: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>

Crate generate.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");
?>